Track downloads with PHP?

Miscellaneous Forums/General Discussion/Track downloads with PHP?

I use this

$product_name=array('product1,product2');
$os_version=array('Windows XP','Mac OS X');
$file_name=array('product1.exe','product2.zip');

function redirect($url, $seconds = FALSE)
{
    if (!headers_sent() && $seconds == FALSE)
    {
        header("Location: " . $url);
    }
    else
    {
        if ($seconds == FALSE)
        {
            $seconds = "0";
        }
        echo "<meta http-equiv="refresh" content="$seconds;url=$url">";
    }
}


include("header.php");
$product=$_GET['prod']-1;
$osversion=$_GET['os']-1;
include("basic.php");
$cnt="download_counter_".$product."_".$osversion.".txt";
$open=@fopen($cnt,'r+');
$counter=@fread($open,filesize($cnt));
@fclose($open);
$counter++;
$write=fopen($cnt,'w+');
fputs($write,$counter);
fclose($write);

echo $product_name[$product]." (".$os_version[$osversion].")";
	  $prodotto_download=$file_name[$product+$osversion];
redirect("download/".$prodotto_download,5);



In the main page put the link to your program like this:

download.php?prod=1&os=1

Of course prod=1 and os=1 are the indexes of the arrays (products, OsVersion, file name) your previously create.

Basically its intercept the link to a file and increments by one a .txt file (download_counter_0_0.txt), then redirect() the input to the original file...
Well it works, but for many products it's better use a MySql database.