简体   繁体   中英

limit curl requests per user

I searched all over, but have not seen anything related to this question. I have a curl script that obtains a random generated string of 10 characters from a website. I have received permission from the website owner to fetch this data and display it on my blog. My question is this when a user comes to my website it displays the string and if they refresh the page it will display a new string. How can I limit one string per user so that they cannot refresh over and over to obtain multiple strings?

Here is an example curl upon which my code is based:

$url = "http://m.www.yahoo.com/";  
$ch = curl_init();  

curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_HEADER, 0);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  

$output = curl_exec($ch);  

preg_match('/<dl class="markets clearfix strong small">(.*)<\/dl>/is', $output, $matches);  
echo $matches[0];  

curl_close($ch);  

maybe you can use session variable to check if the user has already got a string.

function startSession(){
    $id = session_id();
    if ($id != '')
        session_write_close();

    if (isset($_COOKIE['PHPSESSID']) && $id != $_COOKIE['PHPSESSID']) {
        session_write_close();
        session_id($_COOKIE['PHPSESSID']);
    }

     session_start();
}

//Start your sessions

startSession();
if (!$_SESSION['UserGotAString']) {
    echo "Some Error msg"
    return;
}
session_write_close();

//Request to Yahoo and regex match comes here..

// Start a session
startSession();
$_SESSION['UserGotAString'] = true
echo $matches[0];
session_write_close();

Im not sure if this a good method. Also if you want to reset the session variable base on timer check this link Set timeout in php

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