Tip: How to detect with PHP if iPhone visits your site

Saturday, January 30, 2010 2:17
Posted in category Cómo, Mozilla, PHP, Programación, Tips, iPhone

Currently I’m developing some websites which “can be visited from iPhone” [the customer say that], the big thing is: there are some javascripts functions to detect it but when you use it with jQuery this “lock” the effects.

The solution is so easy, you just need to use this PHP script:

PHP<5.3.0

if (ereg('iPhone',$_SERVER['HTTP_USER_AGENT']))
{
header("Location:/iphone.html");
}

PHP>=5.3.0

if (preg_match("/iPhone/",$_SERVER['HTTP_USER_AGENT']))
{
header("Location:iPhone.php");
}

Take Care: Is Important if you have PHP bigger than 5.3.0 you need to use preg_match instead of ereg, becuase ereg is DEPRECATED in PHP 5.3.0 and will be delete in PHP 6.0.0

The function is so easy, because it detect if appears the “iPhone” word in the string HTTP_USER_AGENT. Anyway, this is easily handly in Firefox just need to change the User Agent in about:config writting:

Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; es-es) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16

You need to restart firefox when you do this

[With the UserAgent changed even my blog load in iPhone Format]

[remember to put back the configuration when you finish to play with it  (Firefox/x.x.x) [x.x.x = your version...] :P ]

You can see here the example [just add a x to .php to see the source code]

And here the screenshoot from the iPhone

Cheers, and I hope this could help you [soon i will publish it in ASP.net]

You can leave a response, or trackback from your own site.

One Response to “Tip: How to detect with PHP if iPhone visits your site”

  1. Maximiliano Marin says:

    February 3rd, 2010 at 23:02

    Hey! It is an excellent idea write posts in spanish and then translate them to english. I will take your idea by advice.

    Excellent post!

Leave a Reply