Time Limitted Downloads?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Riotblade
Forum Newbie
Posts: 3
Joined: Fri May 26, 2006 11:20 pm

Time Limitted Downloads?

Post by Riotblade »

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
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

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? :-D
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

create a database with the following fields:

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() );
Then, to check if it has been within 20 seconds,

Code: Select all

SELECT COUNT(*) AS count FROM downloads WHERE ip = 'XXX.XXX.XXX.XXX' AND timestamp > NOW() - INTERVAL 20 SECOND;
if the above query's count value is > 0, then don't let um download the file.
Riotblade
Forum Newbie
Posts: 3
Joined: Fri May 26, 2006 11:20 pm

Post by Riotblade »

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? Very Happy
Well, I do not want a login system :)
Todd_Z wrote:create a database with the following fields:

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() );
Then, to check if it has been within 20 seconds,

Code: Select all

SELECT COUNT(*) AS count FROM downloads WHERE ip = 'XXX.XXX.XXX.XXX' AND timestamp > NOW() - INTERVAL 20 SECOND;
if the above query's count value is > 0, then don't let um download the file.
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.

Thanks for the help guys.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

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 matter ;) so whichever way :-D
Riotblade
Forum Newbie
Posts: 3
Joined: Fri May 26, 2006 11:20 pm

Post by Riotblade »

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 matter ;) so whichever way :-D
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?

Sorry, I kinda skipped over everything else after I read login the first time. This may just work.

Thanks!
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

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)

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'];
?>
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.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

touche! points to you sir, I didn't think it through that far..guess I just write too many login systems..constantly..it's my life work I etll you..
Post Reply