简体   繁体   English

限制每个用户的 curl 请求

[英]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.我有一个 curl 脚本,它从网站获取随机生成的 10 个字符的字符串。 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:这是我的代码所基于的示例 curl:

$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.也许你可以使用 session 变量来检查用户是否已经得到了一个字符串。

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此外,如果您想根据计时器重置 session 变量,请检查此链接Set timeout in php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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