my web host restricts traffic volumes, if it is exceeded they take your site down.

I have a number of files i want to make available to people browsing my website but want to avoid the site being cut off due to excess traffic - what i need is a simple script that will allow a certain number of files to be downloaded during a 24hr period - then restrict access / re-direct to another page when that number is reached. the script should reset everyday so the next days browsers can download the files.

im thinking write a simple count variable to a text file and perform a check each time the download page is called - this i can probably do, but how do i reset it at midnight the next day?

thanks for any help

    You'll need a cron job to do it... unless you want to be awake every night at midnight 🙂

    If you have a db to access, you could just track hits in it, and do a count on entries with todays date stamp, then show or redirect. Maybe setup a way to flush the tables once a month or so (so you can dispute a bill if you get one...)

      i thought i might be able to do it without the db but shouldnt be a problem, thanks for replying, writing the code now 😃

        <?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.

          a bit of cunning lateral thinking.. nice 😃

          a further thought - is there any way of actually logging traffic volumes? because my simplistic file numbers doesnt take into account numbers of page impressions, or where a user cancels a download and therefore doesnt consume traffic...

          thanks guys

            I don't believe there is a way to track if the user actully finished the download. If there is I am unaware of it, so I must be fairly complex.

              Write a Reply...