Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP and Browser User Agent

Posted on 2011-09-19
11
Medium Priority
?
385 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1000 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 84

Accepted Solution

by:
Dave Baldwin earned 1000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

670 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