CURL用户代理 [英] The CURL User Agent

查看:182
本文介绍了CURL用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么,如果客户端是curl,那么如何检查使用codeigniter,然后返回不同的东西呢?

解决方案

在使用cURL时可以伪造用户代理,因此,当您知道它是cURL请求时,根据用户代理发送的内容无关紧要。



例如:我最近写了一个应用程序,它从google获取一个url的pagerank。现在Google不喜欢这样,所以它只允许某个用户代理访问其pagerank服务器。解?



故事的道德:cURL用户代理是绝对不可靠的。



如果你仍然想要这样做,那么你应该能像正常一样获得传递的用户代理

  $ userAgent = $ _ SERVER ['HTTP_USER_AGENT']; 

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



dumpx.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 
var_dump($ _ SERVER);
?>

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

 'HTTP_USER_AGENT'=>案例2:    /localhost/dumpx.php?u=n\">http://localhost/dumpx.php?u=n  



否$ _SERVER ['HTTP_USER_AGENT']



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


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

解决方案

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

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.

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

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'];

EDIT A quick test proved this:

dumpx.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
    var_dump($_SERVER);
?>

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

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

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

No $_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用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆