Solved

PHP and Browser User Agent

Posted on 2011-09-19
11
366 Views
Last Modified: 2012-08-13
Hi all,

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

0
Comment
Question by:error77
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 8

Expert Comment

by:Rik-Legger
ID: 36563648
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

0
 

Author Comment

by:error77
ID: 36563745
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?
0
 

Author Comment

by:error77
ID: 36565063
Anyone please?
0
 
LVL 7

Expert Comment

by:boon86
ID: 36566120
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.
0
 

Author Comment

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

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Expert Comment

by:MattJellings
ID: 36569597
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
0
 

Author Comment

by:error77
ID: 36570049
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?

0
 
LVL 7

Expert Comment

by:boon86
ID: 36571556
<?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);
?>
0
 
LVL 7

Expert Comment

by:boon86
ID: 36571558
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

0
 
LVL 4

Assisted Solution

by:MattJellings
MattJellings earned 250 total points
ID: 36571978
Error 77

From your last.comment it ie.not.working because you are changing the user agent of php itself but then echoing the user agent of you own request.  For the second code example to work you need to change the user agent of your browser. You can do this either by using a plugin or by using a different browser.
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 250 total points
ID: 36579038
ini_set('user_agent'... sets the user_agent that PHP will use when it makes URL call to get a file.  http://www.php.net/manual/en/filesystem.configuration.php#ini.user-agent  It has nothing to do with curl or your current page and browser.  To see what curl sends, you would have to create a PHP page on another server that curl can access so the page can record what curl sends.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now