Skip to content

Instantly share code, notes, and snippets.

@MikeRogers0
Created June 10, 2012 22:33
Show Gist options
  • Save MikeRogers0/2907549 to your computer and use it in GitHub Desktop.
Save MikeRogers0/2907549 to your computer and use it in GitHub Desktop.
Simple hit counter
<?php # File created on 25th April 2009 by Mike Rogers (http://www.fullondesign.co.uk/).
## Start defining constants ##
define(LOG_URL, '/home/user/download_logs/'); // Put the location of where you want to put the logs. Make sure this is absolute
# $_GET['ID'] – this is the ID of the file we want. It gets the value from the URL.
## Now set the data you wish to use – this can be moved to an include if you want ##
$file[0] = 'http://www.example.com/file.pdf';
## Define the functions required to update the file.
function update_file($filename, $value){
if(is_writable($filename) && is_readable($filename)){
if(file_put_contents($filename, $value)){
return TRUE;
}
}
return NULL;
}
function pull_file($filename){
if(is_writable($filename) && is_readable($filename)){
return file_get_contents($filename);
}
return NULL;
}
function rebuild_file($filename){
if(is_writable($filename) && is_readable($filename)){
file_put_contents($filename, '1');
}
}
if(is_numeric($_GET['ID'])){ // Meaning the Data sent is safe.
// Build log file URL
$filename = LOG_URL.$_GET['ID'].'.log.txt';
$value = pull_file($filename);
header('location:'.$file[$_GET['ID']]);
// Update logs
if(is_numeric($value)){
update_file($filename, ($value+1));
} else { // Meaning the file does not exist or has been messed with.
rebuild_file($filename);
}
} else {
echo 'Sorry, there was an error.';
}
/* If you want to see how many people have downloaded a file, run something like:
# pull_file(LOG_URL.numberishere.'.log.txt');
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment