Link to home
Start Free TrialLog in
Avatar of error77
error77

asked on

PHP and Browser User Agent

Hi all,

How can I change the browser user agent but code please?

Avatar of Rik-Legger
Rik-Legger
Flag of undefined image

For use in curl or what?

For curl it is:

curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');

Open in new window

Avatar of error77
error77

ASKER

I've added this to me php page

<?php

curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))');

?>

Open in new window


It's still telling me I'm using Firefox when the code above should set it to IE 9

Any ideas on why it;s not working please?
Avatar of error77

ASKER

Anyone please?
Hi,

Use firefox then download addon called: User Agent
you can change user agent of your browser to anything you
want to pretend, example:

iphone,
IE8,
IE9,
Safari,
googlebot
msnbot

hope that help.
Avatar of error77

ASKER

I've got that addon but this I need to do it via the web page as it's for a simulator.

Hi error77,

It would depending on whether you are trying to access the web page itself pretending to be a different web browser (user agent) or whether your PHP code retrieves content from a remote resource and you want this process to appear as a different browser (user agent).

I believe if you want to open a page in your web browser (Firefox) but have the web site believe you are using a different user agent then boon86's answer above would be correct.  The alternative would be in your code where you check what user agent the request came from simply override this with the desired user agent for testing.  Another alternative would be to use the browser with the desired user agent.

If, however you would like your PHP code to request content from a remote resource impersonating a specific user agent then the answer from RIk-Legger is correct presuming your are using CURL to retrieve the data.  The alternative if you are using fopen() etc would be to use:

ini_set('user_agent', 'Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US)');

Open in new window


Please note I haven't tested the above suggestions however they have what I've come up with from Google results.

I hope these suggestions help, my apologies if they do not.

Cheers

Matt
Avatar of error77

ASKER

Sorry but none of the examples work...

At the top of my page I have:

ini_set('user_agent', 'Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US)');

Open in new window


Then unde that I have:

 echo $_SERVER['HTTP_USER_AGENT'];

Open in new window


and It's returning:

Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2

and not Internet Explorer ... why is it not working please?

<?php
    $url  = 'http://www.example.com/a-large-file.zip';
    $path = '/path/to/a-large-file.zip';
 
    $fp = fopen($path, 'w');
   
    $ch = curl_init($url);
    $ch = curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    curl_setopt($ch, CURLOPT_FILE, $fp);
 
    $data = curl_exec($ch);
 
    curl_close($ch);
    fclose($fp);
?>
correction:

<?php
    $url  = 'http://www.example.com/a-large-file.zip';
    $path = '/path/to/a-large-file.zip';
 
    $fp = fopen($path, 'w');
    
    $ch = curl_init($url);
     curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    curl_setopt($ch, CURLOPT_FILE, $fp);
 
    $data = curl_exec($ch);
 
    curl_close($ch);
    fclose($fp);
?>

Open in new window

SOLUTION
Avatar of Matt
Matt
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial