<?php
// the data file
$filename = "downloads.dat";
// the number of downloads per day
$limit = "10";
// check to see if file exists, if not die()
if( !file_exists($filename) )
{
echo "<b>Error: </b> The data file(" . $filename . ") doesn't exist!";
die( );
}
// get file contents
$fileContent = file_get_contents($filename);
// split the string. [0] = day number, [1] = # of downloads
$fileContent = explode(":", $fileContent);
// get today's number. # out of 365
$today = date("z");
// if the day in file = today, continue.
if( $fileContent[0] == $today )
{
// if the number of downloads is less than the limit, continue
if( $fileContent[1] < $limit )
{
// ...
// download the file
// ...
$newNum = $fileContent[1] + 1;
$newContent = $today .":". $newNum;
file_put_contents($filename, $newContent);
}
// if number of downloads is = to, or greater than the limit, die( )
else
{
echo "Sorry, but this site has reached it's download limit for today.";
die( );
}
}
// if the day in the file doesn't = today
else
{
$resetContent = $today . ":0";
// reset day to today, and downloads to 0
file_put_contents($filename, $resetContent);
// ...
// redirect to whatever page
// ...
}
?>
make sure you create a file called downloads.dat and put:
64:0
64 is for today(sunday the 6th), but if you set it tomorrow then put 65.
The only problem with this is you need to reset it yearly.