PHP Auto-Bowser Detection
Though not typically recommended, user agent detection can be useful for presentations formatted for the iPhone/iPod touch specifically. This PHP tutorial can be used to detect the user agent of the iPhone browser, allowing you to forward a visitor to the iPhone version of your website.
Below is the PHP:
< ?php
$browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
if ($browser == true) { echo 'Code You Want To Execute'; }
?>
Lets dissect this,
$browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone")
“$_SERVER['HTTP_USER_AGENT']” is a function built into PHP, which gets information about the users browser. You can read more about this function here.
Next, using strpos, another built in function of PHP, we search for a string inside our $browser variable. If strpos finds “iPhone” in $browser, it will let us run the code in side the if statement
if ($browser == true) { echo 'Code You Want To Execute'; }
From here, you may want to extend the if statement to redirect the user to your iPhone microsite, use a different stylesheet or just post a special message to iPhone users. It’s up to you.
We hope this basic tutorial helps with your iPhone microsite development. You may also consider using CSS to detect iPhone visitors as other phones using MobileSafari could be accidentally detected using this method. We’ll cover this topic in another tutorial. As always, let us know what you think.
-Shaun Mackey



(1 votes, average: 4 out of 5)

Comment by Mickie: July 2, 2008 @ 4:22 am
I have a little question: when want to redirect, is this code correct?
if ($browser == true) { echo ‘redirectpage.php’; }
Comment by admin: July 2, 2008 @ 3:42 pm
Hi Mickie,
You can use PHP’s header() function in place of the echo, like so:
<?php
$browser = strpos($_SERVER['HTTP_USER_AGENT'],”iPhone”);
if ($browser == true) { header(”Location: http://www.example.com/“); }
?>
Note that no output can be sent before the function is called, as you will get an error.
Hope that helps!
Comment by Bufo: July 20, 2008 @ 11:15 am
Are you sure that the code above detects the iPod Touch as well as the iPhone? Somehow, I don’t believe that the User Agent for the iPod Touch is “iPhone”.
Comment by admin: July 20, 2008 @ 3:36 pm
Hi Bufo,
The script was tested and works with iPod Touch, but it is counterintuitive as the user agent strings are in fact slightly different.
If you’d like, you can always add specific support for iPod touch by doing another check for “iPod”.
$touchDetect = strpos($_SERVER['HTTP_USER_AGENT'],”iPod”);
Then extend your if statement to check this as well.
Hope that helps!
-Admin
Comment by david: July 23, 2008 @ 9:55 pm
wouldnt it be more practical to use a service like handsetdetection.com so that you can adjust for every handset rather than just the iphone?
(i get that this is an iphone specific site but……)
Comment by Josh N.: August 15, 2008 @ 1:49 pm
“Auto-bowser Dectection” ??!!
Where was this when I played Super Mario Bros?
Comment by AppStoreDeals: October 30, 2008 @ 1:44 pm
Thanks so much for this! On App Store Deals I modified the code a bit to detect iPod and iPhone such as:
Seems to work well! Thanks for the base.
Justin @ http://appstoredeals.com