php CURL 用户代理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8194795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:08:03  来源:igfitidea点击:

The CURL User Agent

phpcodeignitercurluser-agent

提问by macintosh264

So how can I check using codeigniter if the client is curl, and then return something different for it?

那么如何使用 codeigniter 检查客户端是否为 curl,然后为其返回不同的内容?

回答by Pranav Hosangadi

You can fakethe user-agent when using cURL, so it's pointless depending on the user-agent sent when you KNOW it's a cURL request.

您可以在使用 cURL 时伪造用户代理,因此当您知道它是 cURL 请求时发送的用户代理是毫无意义的。

For example: I recently wrote an app which gets the pagerank of a url from google. Now Google doesn't like this, so it allows only a certain user agent to access its pagerank servers. Solution? Spoof the user-agent using cURL and Google will be none the wiser.

例如:我最近写了一个应用程序,它从谷歌获取一个 url 的 pagerank。现在谷歌不喜欢这样,所以它只允许某个用户代理访问它的pagerank服务器。解决方案?使用 cURL 欺骗用户代理,谷歌也不会更聪明。

Moral of the story: cURL user agents are JUST NOT reliable.

故事寓意:cURL 用户代理并不可靠。

If you still want to do this, then you should be able to get the passed user agent just like normal

如果您仍然想这样做,那么您应该能够像往常一样获得通过的用户代理

$userAgent=$_SERVER['HTTP_USER_AGENT'];

EDITA quick test proved this:

编辑一个快速测试证明了这一点:

dumpx.php:

转储.php:

<?php

    $url="http://localhost/dump.php";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    if($_GET['u']==y) {  
    curl_setopt($ch, CURLOPT_USERAGENT, "booyah!");
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    //curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
    curl_setopt ($ch, CURLOPT_HEADER, 0);
    $exec=curl_exec ($ch);
?>

dump.php:

转储.php:

<?php
    var_dump($_SERVER);
?>

Case 1: http://localhost/dumpx.php?u=y

案例 1:http://localhost/dumpx.php?u=y

 'HTTP_USER_AGENT' => string 'booyah!' (length=7)

Case 2: http://localhost/dumpx.php?u=n

案例 2:http://localhost/dumpx.php?u=n

No $_SERVER['HTTP_USER_AGENT']

没有 $_SERVER['HTTP_USER_AGENT']

This proves that there is no default user agent for curl: it will just not pass it in the request header

这证明 curl 没有默认的用户代理:它不会在请求头中传递它

回答by Oroboros102

If you want to detect bots you can not rely on user agent. Best practices are:

如果你想检测机器人,你不能依赖用户代理。最佳做法是:

  1. Check, that your visitor runs js (not all human users also do).
  2. Check, that your visitor loads additional files linked to webpage (css, images, etc.)
  3. Check visitor timeouts. Humans usualy don't load 10 pages per second.
  1. 检查您的访问者是否运行 js(并非所有人类用户都这样做)。
  2. 检查您的访问者是否加载了链接到网页的其他文件(css、图像等)
  3. 检查访客超时。人类通常不会每秒加载 10 页。

回答by Kamil Lach

cURL stands for - Client URL Library and the whole point of it is to be able to make requests that are identical to what a client would make.

cURL 代表 - 客户端 URL 库,它的全部意义在于能够发出与客户端发出的请求相同的请求。

The only thing you can do is detect the information that is part of the request, such as the IP address, HTTP Request Headers, cookies/session id cookie, URL (path/page), and any post/get data. If the person using curl to make the request is doing it from an expected IP address and is supplying any expected header/cookie/token/URL/post/get values, then you would not be able to distinguish a curl request from a browser making the request.

您唯一能做的就是检测作为请求一部分的信息,例如 IP 地址、HTTP 请求标头、cookies/会话 ID cookie、URL(路径/页面)和任何 post/get 数据。如果使用 curl 发出请求的人是从预期的 IP 地址执行此操作并提供任何预期的 header/cookie/token/URL/post/get 值,那么您将无法区分 curl 请求和浏览器发出的请求请求。

回答by MacMac

You can spoofor set a custom user agent header when using cURL, so it wouldn't be reliable.

使用 cURL 时,您可以欺骗或设置自定义用户代理标头,因此它不可靠。

Otherwise, you can do this:

否则,您可以这样做:

if(strtolower($this->input->server('HTTP_USER_AGENT', true)) == 'curl')
{
    // Is using cURL
}

This would only occur if the cURL request contained curlin the user agent header.

仅当 cURL 请求包含curl在用户代理标头中时才会发生这种情况。

As far as I know, there is no default user agent set when doing a curl request.

据我所知,在执行 curl 请求时没有设置默认的用户代理。