【问题标题】:Php rate limit doesn't work with curl requestsPHP 速率限制不适用于 curl 请求
【发布时间】: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?

标签: php http curl


【解决方案1】:

解决方案

我检查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"

【讨论】:

    猜你喜欢
    • 2012-09-03
    • 2021-08-03
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 2018-11-05
    相关资源
    最近更新 更多