Time Limitted Downloads?
Moderator: General Moderators
Time Limitted Downloads?
I've searched all over google for an idea on how this works.
I'm trying to setup a page where people can only download a file every 20 seconds or so. The download is called from a function which checks if they have downloaded anything within the past 20 seconds.
My idea is to get their ip address and insert it into a db with the date/time.
What's puzzling me is how I can check and update the database every ten seconds. I was thinking of using a cron job but the lowest it can go to is minutes.
I want to hear the opinions of skilled programmers on how to do this the most efficient way.
Thanks
I'm trying to setup a page where people can only download a file every 20 seconds or so. The download is called from a function which checks if they have downloaded anything within the past 20 seconds.
My idea is to get their ip address and insert it into a db with the date/time.
What's puzzling me is how I can check and update the database every ten seconds. I was thinking of using a cron job but the lowest it can go to is minutes.
I want to hear the opinions of skilled programmers on how to do this the most efficient way.
Thanks
-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
erm..make a login system..then set a session variable called last download time, set it the first time they download something and insert it into the database. then check when they go to download if difference in time is greater than 20 seconds. so..to be more detailed..
when they first login set a session variable for download time equal to 0 along with a field in the database.
whenever they go to download a song check current time verses last downloaded time in the session, if difference is greater than 20 seconds download and then update the time stamps, if not display an error with how much longer they have to wait.
sound good?
when they first login set a session variable for download time equal to 0 along with a field in the database.
whenever they go to download a song check current time verses last downloaded time in the session, if difference is greater than 20 seconds download and then update the time stamps, if not display an error with how much longer they have to wait.
sound good?
create a database with the following fields:
ip address
datetime
then, when a file is downloaded, do an insert like:
Then, to check if it has been within 20 seconds,
if the above query's count value is > 0, then don't let um download the file.
ip address
datetime
then, when a file is downloaded, do an insert like:
Code: Select all
INSERT INTO downloads ( ip, timestamp ) VALUES ( 'XXX.XXX.XXX.XXX', NOW() );Code: Select all
SELECT COUNT(*) AS count FROM downloads WHERE ip = 'XXX.XXX.XXX.XXX' AND timestamp > NOW() - INTERVAL 20 SECOND;Well, I do not want a login systemerm..make a login system..then set a session variable called last download time, set it the first time they download something and insert it into the database. then check when they go to download if difference in time is greater than 20 seconds. so..to be more detailed..
when they first login set a session variable for download time equal to 0 along with a field in the database.
whenever they go to download a song check current time verses last downloaded time in the session, if difference is greater than 20 seconds download and then update the time stamps, if not display an error with how much longer they have to wait.
sound good? Very Happy
That's what I was thinking of. Only problem is that it gets checked when the function is called. I want it to check every 10 seconds regardless of traffic.Todd_Z wrote:create a database with the following fields:
ip address
datetime
then, when a file is downloaded, do an insert like:
Then, to check if it has been within 20 seconds,Code: Select all
INSERT INTO downloads ( ip, timestamp ) VALUES ( 'XXX.XXX.XXX.XXX', NOW() );
if the above query's count value is > 0, then don't let um download the file.Code: Select all
SELECT COUNT(*) AS count FROM downloads WHERE ip = 'XXX.XXX.XXX.XXX' AND timestamp > NOW() - INTERVAL 20 SECOND;
Thanks for the help guys.
-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
Hey, your's is actually a pretty good idea. I can just replace session with IP since I don't really care if they proxy it. So everytime somebody downloads a file, the IP and date/time gets inserted. Then the next time they download a file, check if the current time is at least 20 seconds greater than their last downloaded time right?Charles256 wrote:ip isn't the most reliable method, hence why i recommended a login system. however, 20 second limitation isn't much and i spose it won't really matterso whichever way
Sorry, I kinda skipped over everything else after I read login the first time. This may just work.
Thanks!
Why use a database at all? If you're going to store session data, then use the session variables, e.g.
(tested -run this script and refresh every few seconds)
Simple enough? As far as I'm concerned, the IP is irrelevant. The session variable check is enough to throttle the user. If you have to set the user IP or username or whatever, you of course can do this also and check it within the if () statement, or create a new if() { } else { } block to check the user's IP first, then start the clock.
(tested -run this script and refresh every few seconds)
Code: Select all
<?php
// start the session, of course
session_start();
$time= time(); // a standard base of time for the script
$time_minus= $time -20; // the time 20 seconds ago
// check the $_SESSION variable 'dl_time'
// if it's set, and the time set is >= the time now - 20 seconds, disallow
if ( !empty($_SESSION['dl_time']) && ( $_SESSION['dl_time'] >= $time_minus ) ) {
// you can't download
echo 'You cannot download yet...<br />';
}
else {
// you can download
// reset the 'dl_time' variable, start the clock over
$_SESSION['dl_time'] = $time;
// download handling function
echo 'Starting the download...<br />';
}
echo $time .'<br />';
echo $_SESSION['dl_time'];
?>-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm