Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The CURL User Agent

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

like image 349
macintosh264 Avatar asked Nov 19 '11 14:11

macintosh264


People also ask

Does Chrome use curl?

From Chrome and Edge Paste that in a shell to get a curl command line that makes the transfer. This feature is available by default in all Chrome and Chromium installations.

How do I get curl request on Chrome?

Open Chrome Developer Tools, go to Network tab, make your request (you may need to check "Preserve Log" if the page refreshes). Find the request on the left, right-click, "Copy as cURL". But cookie in "Copy as cURL" expires within few minutes.

How do I check my browser curl request?

cURL makes HTTP requests just like a web browser. To request a web page from the command line, type curl followed by the site's URL: The web server's response is displayed directly in your command-line interface. If you requested an HTML page, you get the page source -- which is what a browser normally sees.

What is the Chrome user agent?

A browser's User-Agent string (UA) helps identify which browser is being used, what version, and on which operating system. When feature detection APIs are not available, use the UA to customize behavior or content to specific browser versions.


1 Answers

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

like image 187
Pranav Hosangadi Avatar answered Sep 26 '22 16:09

Pranav Hosangadi