mlsp numis network training
 

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.

PS: Learn My Simple, 4-Step Process For Setting Up A Profitable Web Funnel (No Tech-knowledge needed) Click Here For A Free Video.

Shaun bio imageShaun Mackey
Web Profit Strategies
Check Out My Fan Page: http://facebook.shaunmackey.com
Friend Me On Facebook: http://www.facebook.com/shaunmackey1
Follow Me On Twitter: http://twitter.com/shaunmackey

Comments

  1. MikusP says:

    Hi! This is an incredibly basic question but I’m new to web coding and PHP. I’m already using a PHP script for my contact page to forward on simple email messages but this is activated by the users interaction. Just wondering how this PHP file is initiated upon accessing the website index.html? Thanks.

    • Shaun Mackey says:

      You need to rename the index.html file with a .php extension if you want the PHP code contained inside to execute in a browser.

    • Carlos says:

      I came here looking for something else but I wanna help you or somebody who need it in near future.

      You can modify the directives for handle html like php file with .htaccess (just open it as text file)

      For web servers using PHP as apache module:
      Add this line:

      AddType application/x-httpd-php .html .htm

      For web servers running PHP as CGI:

      AddHandler application/x-httpd-php .html .htm

      Good luck.

Speak Your Mind

*

 Subscribe to My Newsletter 

 
Copyright 2013 Shaun Mackey - All Rights Reserved
No part of this website may be reproduced without prior written concent of Shaun Mackey.

Privacy Policy | Earnings Disclaimer | Terms | Google+