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.

You may want to like this now ➡➡

To your continued success in life and business.

Was this post helpful? If so, I would totally appreciate you commenting below and clicking "like" above.

Shaun Mackey
I help people make websites better, faster and more profitable for their online business.
Contact me here.


Enter Your Comments Below: