开发者

limit curl requests per user

开发者 https://www.devze.com 2023-03-19 12:54 出处:网络
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 t

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:

$u开发者_如何学编程rl = "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

0

精彩评论

暂无评论...
验证码 换一张
取 消