Solved

PHP and Browser User Agent

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

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

0
Comment
Question by:error77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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
 
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 83

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 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