PHP Hack – How to Detect iPhone, iPod, Andoird, Palm Pre Browsers!

One of our readers Titus just alerted me that Zedomax.com on smartphone browsers such as his iPhone will cause our e-mail newsletter lightbox to appear and make it almost impossible to get rid of.

Therefore, I’ve just put in a simple PHP code that will detect iPhone, iPod, Android, and Palm Pre browsers so there’s no more of that.
Try PHP code something like below:

<?php
$iphone = strpos($_SERVER[‘HTTP_USER_AGENT’],”iPhone”);
$android = strpos($_SERVER[‘HTTP_USER_AGENT’],”Android”);
$palmpre = strpos($_SERVER[‘HTTP_USER_AGENT’],”webOS”);
$ipod = strpos($_SERVER[‘HTTP_USER_AGENT’],”iPod”);
if(!$iphone && !$android && !$palmpre && !$ipod) {
?>
Your code goes here that you want only executed when it’s not a smartphone.

<?php } ?>

Btw, strpos is a simple PHP command that will search for the specified string and return true of false if it exists.

And $_SERVER[‘HTTP_USER_AGENT’] returns the user agent, you can read more about that here.

Of course, you can also use this code to serve mobile-friendly version of your website. So far, I actually haven’t found a good workaround for this site so I will just stick with the regular website until I do.

6 Responses to PHP Hack – How to Detect iPhone, iPod, Andoird, Palm Pre Browsers!

  1. Anonymous says:

    strpos is a php function that returns the position of one string in another, so could be 0 (start of string) or higher.

    use (for example)

    if(strpos($_SERVER[‘HTTP_USER_AGENT’],”iPhone”) > -1) {

    } else {

    }

    to be sure.

    But you can never be sure as any data from the client can be spoofed/faked

  2. Anonymous says:

    Correct use would be:

    if(strpos($_SERVER[‘HTTP_USER_AGENT’],”iPhone”) === false) {

    } else {

    }

  3. max says:

    3 equal signs???

  4. Tanvir Gaus says:

    @Max – 3 equal means identical. == is “is equal” & === is “is identical” in all means. in PHP u can use $a = ‘1’ [here the 1 is char not number] and check it if($a==1) which could return true
    but using if($a===1) means the value for a should be 1 and number 1 so it will return false.

  5. Tanvir Gaus says:

    Can I have a list of Smartphone device/browsers to check with? there are thousands of vendors and thousands of headers 🙁 I need to detect the most devices and browsers that supports most JS and CSS.

  6. Mi says:

    Function “preg_match” is more useful than “strpos” 🙂

Leave a Reply

Your email address will not be published.


Other interesting stuff at Zedomax.com: , , , , , , , ,


Related News and Resources