简体   繁体   中英

php limit request per second

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 ?

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();
}

?>

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM