0

Hello i'm having trouble with making like a simple waiting time between 2 php requests like... i have this code (for example) :

readfile($link);

the visitor request this php code to download a file ... but within 10 seconds he requests another one ... i want to make a limit for the time like if he requested once he has to waiting from the first download to the allowance to the second request (download) 30 seconds ... any idea of how could this be done ? in php ... thank you in advance :)

edit : i tried with the help of the post below but did not work

if (!isset($_SESSION['last_download'])) $_SESSION['last_download'] = 0; {
if (time() - $_SESSION['last_download'] > 10){
    $_SESSION['last_download'] = time();
    echo $_SESSION['last_download'];
    }else {echo"".time() - $_SESSION['last_download']."";}}

but its not saving the session ... any help ?

1
  • I think check for same IP address can handle this problem. But it is not the heaven beauty solution. Commented Oct 21, 2011 at 17:29

2 Answers 2

2

but its not saving the session

Did you forget to include session_start()?

Try

<?php

session_start();

if (isset($_SESSION['last_download']))
{
    if (time() - $_SESSION['last_download'] > 10)
    {
        // allow download
        $_SESSION['last_download'] = time();
        echo $_SESSION['last_download'];
    }
    else
    {
        echo time() - $_SESSION['last_download'];
    }
}
else
{
    // session not set yet, set it now
    $_SESSION['last_download'] = time();
}

?>
Sign up to request clarification or add additional context in comments.

1 Comment

you are right i forgot to start the session :P thank you for your helpp
1

Just save last download time into a session

<?php
if (!isset($_SESSION['last_download'])) $_SESSION['last_download'] = 2; //time in past
if ($_SESSION['last_download'] && time() - $_SESSION['last_download'] > 10){
    $_SESSION['last_download'] = time();
    //allow download
}

3 Comments

Just keep in mind that this will only prevent users from downloading within the timeframe on the current session. For instance, if they opened a new browser they would be able to download the file without the existing threshold. This is why it's better to lock down to the signed in accounts, not their sessions.
no problem as he have to wait for the search to be searched again ... btw that code above did not work :/
Also keep in mind this won't block most malicious bot attacks because they will most likely create a new session for each request.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.