a download limitation script that defies logic!

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I thought I'd take the time to make a download limitation script, but even
though the idea of this script is 'simple', it 'simply' refuses to work
entirely properly.

Basically I setup a real quick timelimit, $time_limit should mean 'within
the last 20 seconds'. What I am looking for is the following:

a user requests a link via the script, i've got all that part right. what we
do first is check to see if there is info in the db about the user who is
identified by their ip. if so, use it to calculate stuff, if not, download
the file, record their ip and update the download counter by 1.

what i want to happen is only allow a certain ip to download a max of 2
files per give time period.

the problem is the script 'works' as expected if i replace

------------------------------------------------
else
                {
                        mysql_query("UPDATE ip_limit SET downloads = 0 WHERE
userip = '$userip'");
                        //$dl = true;
                }
------------------------------------------------
with
------------------------------------------------
else
                {
                        mysql_query("UPDATE ip_limit SET downloads = 0 WHERE
userip = '$userip'");
                        echo "we updated the db successfully!";
                }
------------------------------------------------
.
But if i replace the echo "we updated the db successfully!"; with $dl =
true, (because I want there to be a download right after this reset, rather
than just a notice of a download-record-reset), then all i get is 1
download, then timeout, then 1 download, recurring.

This just defies the laws of logic, or i am insane. Here is the code snippet
that deals with the limitations (followed by the download script in "if
($dl)" - for any brave soul out there. Feel free to rewrite or suggest code
changes, i'm all ears!

------------------------------------------------
$dl_limit_perday = 2;
$time_limit = time() - 20;
 
db_connect($dbhost, $dbname, $dbuser, $dbpass);
 
$dl_limit_perday = 2;
$time_limit = time() - 20;
 
$get_ip_info = mysql_query("SELECT UNIX_TIMESTAMP(last_access),downloads
FROM ip_limit WHERE userip = '$userip'");
$ip_info = mysql_fetch_array($get_ip_info);
$last_access = $ip_info['UNIX_TIMESTAMP(last_access)'];
$downloads = $ip_info['downloads'];
$time_to_wait = $last_access - $time_limit;
$has_hit_dl_limit = ($downloads >= $dl_limit_perday) ? true : false;
$has_hit_timeout_limit = ($last_access > $time_limit) ? true : false;
 
// if($num_dl > 3) { if($time_limit > $blah) { reset_downloads(); 
process_download(); } else { echo "GTFO" } } else { process_download(); }
        
        if($has_hit_dl_limit)
        {
                if($has_hit_timeout_limit)
                {
                        $dl = false;
                        print('<h1>Too many downloads today!<br />');
                        print "Time limit: {$time_limit}<br />Last time
accessed: {$last_access}<br />";
                        print "Time to wait = $time_to_wait seconds</h1>";
                        exit;
                }
                else
                {
                        mysql_query("UPDATE ip_limit SET downloads = 0 WHERE
userip = '$userip'");
                        //$dl = true;
                }
        }
        else
        {
                $dl = true;
        }
 
if ($dl)
{
        mysql_query("REPLACE INTO ip_limit (userip,last_access,downloads)
VALUES ('$userip', NOW(), '$downloads'+1)");
        download_the_file();
}
else
{
        exit, etc
}
------------------------------------------------
-- 
View this message in context: http://www.nabble.com/a-download-limitation-script-that-defies-logic%21-tp18132754p18132754.html
Sent from the PHP - General mailing list archive at Nabble.com.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux