I have a file (.zip, .msi etc) on our server that we try and protect as much as possible. I know sessions can be spoofed, as well as referrer. We either require you to come from our download page (where session starts) or from one specific partner. I have all of this working. I wanted to insert Google Analytics but Ive read inserting js/html will corrupt the download. I would like to just create then write to a txt file and capture the date of each download and create a running count of total downloads. Here is my code to force the download - this is in a file that is directly called and doesn't take user off the partner website:
<?php
session_start();
$ref = $_SERVER['HTTP_REFERER'];
$refData = parse_url($ref);
if(time() - $_SESSION['time'] < 2000 || ($refData['host'] == 'partnersite.com')) {
$path = '/var/www/vhosts/example.org/dir/dir/dir/App.Install.de-de.msi';
$mm_type="Content-type: application/x-ole-storage";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
readfile($path);
//try and write to file and count downloads
$current_count = file_get_contents('count');
$f = fopen('count', 'w+');
fwrite($f, $current_count + 1);
fclose($f);
exit();
} else { ?>
<?php header("Location: https://example.org/404.php");
die(); ?>
<?php }
?>
Again, this works exactly like I want it to in forcing and 'softly' protecting the download from hotlinking. Somewhere in there I need to create "count.php" write to it w/o over writing and then close it.