【发布时间】:2018-05-30 18:15:19
【问题描述】:
我使用这个功能来限制在互联网上冲浪的 http 请求:
session_start();
if (isset($_SESSION['LAST_CALL'])) {
$last = strtotime($_SESSION['LAST_CALL']);
$curr = strtotime(date("Y-m-d h:i:s"));
$sec = abs($last - $curr);
if ($sec <= 2) {
die('Rate Limit Exceeded'); // rate limit
}
}
$_SESSION['LAST_CALL'] = date("Y-m-d h:i:s");
它适用于浏览器
但如果我尝试使用 curl 发出请求:
curl http://localhost/project/p.php
什么都没发生
我如何更新它以使速率限制也对 curl 有效?
解决方案
我检查cookie是否被客户端接受,如果没有退出:
setcookie('test', 1, time()+3600);
if(count($_COOKIE) == 0){
die ("Cookie Not Enabled");
}
使用 cookie 发出 curl 请求并保存 cookie 文件,然后我使用 cookie 发出另一个请求:
curl http://localhost/project/p.php -c cookie-jar.txt
curl http://localhost/project/register.php --cookie "PHPSESSID=g90tqc0hvp6sodods9jisss912"
【问题讨论】:
-
会话需要 cookie 才能工作,因此如果您想对不支持 cookie 的客户端进行速率限制,则需要将速率信息存储在
$_SESSION以外的位置。 -
所以我可以在 php 中添加一个检查客户端是否接受 cookie 的函数?以及如何使用 curl 设置接受 cookie?