1

I would like to set a User Agent for an iFrame as iPhone. I found this code:

navigator.__defineGetter__('userAgent', function(){
return 'foo' // customized user agent
});

navigator.userAgent; // 'foo'

from here.

I am wondering how to insert this with my code: PHP/HTML:

<div class="holder">
<form action="page.php" method="post">
<label>
http://
</label>
<input type="text" name="url" value="<? echo $_POST['url']; ?>" />
<input type="submit" value="Anschauen" />
</form>
</div>
<div class="iphone">  
<iframe src="<? echo 'http://'. $_POST['url']; ?>"></iframe>
</div>

If the above script doesn't work is there any possibility to use AJAX or cURL, if yes could anyone give an example for my code, since I don't know how to code AJAX or cURL.

Community
  • 1
  • 1
Deproblemify
  • 3,340
  • 5
  • 26
  • 41

1 Answers1

3

You are not able to set the User Agent string from PHP side to affect an iframe. User Agent header is a client identifier sent by the browser on each request. Some browsers allow you to manually modify it, but not for individual frames.

The solution you found is likely to be useless as it uses JavaScript to modify the User Agent string. The result of this modification will not be sent to the server that returns the contents of your iframe.


EDIT

OP mentioned in comments he wanted to build an iframe to preview websites in "iPhone Mode".

What you're looking at is going to be quite problematic for you. Several reasons:

  1. No desktop browser is able to perfectly replicate iOS browser behaviour. iOS version of Safari is Apple's custom WebKit build and (theoretically) no other browser, even WebKit ones, will behave the same. You have to take into account touch events, Apple's own tweaks, orientation changes, etc.

  2. If you're going for an iframe solution, you are not able to send a header identifying that you're using an iPhone within the iframe. (There's a workaround, read point 2 below.)

What can be done

  1. Modern websites, instead of sniffing User Agent header (browser identifier) are simply responsive and adapt to the width of the screen. This helps you, because these website don't rely on User Agent header to identify browser and such a website will simply adapt the design to whatever dimentions the iframe has. In you go for this simple approach you will end up with something like this iPhone 4 Browser Simulator.

  2. You might use xHR to make an AJAX request. xHR allows you to modify the headers that are sent with the request and should allow you to change the User-Agent header. I haven't tested it so I don't know if it's allowed to change User Agnet in xHR but it might be worth a try.
    The response would be then returned and you could put that output in the iframe. Unfortunetely it is very likely that the links within the content returned will be broken, because they will no longer be attached to a URL. You would have to have your own script go through the response contents and fixes the links. Ideally all the clicks would have to be intercepted by your script, too, in order to make another xHR request with iPhone's User Agent header.

As you can see, there is a way for you to make some sort of emulation (not simulation), but you'll have to either rely on websites' responsiveness or create a script that requests pages with custom User Agent header (assuming it's allowed).

Community
  • 1
  • 1
Michal M
  • 9,322
  • 8
  • 47
  • 63