Detect mobile browser/user agent with PHP (iPad, iPhone, Android, BlackBerry, WP7 and others)

Posted by on Feb 2, 2011 in PHP | 79 Comments

With all this load of smartphones and tablets, it is mandatory for us as developers to optimize our content as much as possible so everybody can see it, no matter what device they are using. This is why I wrote a small but powerful function to properly detect a wide range of mobile devices and redirect to an special content accordingly.

Lets take a look:

function detect_mobile()
{
	if(preg_match('/(alcatel|amoi|android|avantgo|blackberry|benq|cell|cricket|docomo|elaine|htc|iemobile|iphone|ipad|ipaq|ipod|j2me|java|midp|mini|mmp|mobi|motorola|nec-|nokia|palm|panasonic|philips|phone|playbook|sagem|sharp|sie-|silk|smartphone|sony|symbian|t-mobile|telus|up\.browser|up\.link|vodafone|wap|webos|wireless|xda|xoom|zte)/i', $_SERVER['HTTP_USER_AGENT']))
		return true;

	else
		return false;
}

This function can detect most (if not all) of all the standard “dumb” phones, smartphones, including the iPhone, iPad, Android and Windows Phone 7. Lets say you want to redirect your users to your blog if they are accessing your home page from a mobile device:

$mobile = detect_mobile();

if($mobile === true)
	header('Location: blog');

Nice and easy!

Changelog:

06/06/2012 – Added PlayBook and Kindle Fire support.
16/01/2012 – Rewritten, shortened. Deleted unnecesary checks. Added Windows CE support.
23/05/2011 – Updated for iPod Touch
25/04/2011 – Updated for Windows Phone 7

24/03/2011 – Updated for Android

79 Comments

  1. Adit
    January 28, 2013

    Nice… thanks bro!

    Reply
  2. Adam
    January 28, 2013

    Thanks for the script.

    Reply
  3. BGN
    February 13, 2013

    Hi, I’m viewing this site using blackberry 9900: and I would like to to believe that the version of the site that I’m viewing is a desktop version or tablet version. I think on your function you should devide your function into 3 deciders or four. One for desktop, two for tablets, 3 and 4 for mobiles, mobiles having to sub-heads, full touch screens and normal mobiles. And again. I think it is important to allocate you if statements very well, and if a deviced was first detected as a windows desktop while it is a mobile inside that if statement use a second if stament to see if it’s not a mobile device and if it is redirect it to the 3 and 4th if statement using OOP class. That’s the nice thing about OOP PHP

    Reply
    • Danny Herran
      February 14, 2013

      What you see is a desktop version of the site. I’ve been too lazy to develop a mobile theme for my own site :). Your idea sounds great. It would require some testing because some Android tablets have the same user agent as mobile phones, but thank you for your input. I would certainly take it in consideration for the next update of this function.

  4. Andrew Reed
    March 15, 2013

    I’d use a switch instead of ifs & elseifs

    Reply
  5. Vancers
    June 4, 2013

    Hey Danny, just wanted to chime in with a big THANK YOU! Looks perfect, and I’ll let you know how it works out.

    Reply
  6. chaozz
    July 19, 2013

    I was looking for a simple solution to detect mobile devices for my game, and this works perfectly. Thank you!

    Reply
  7. Rick
    September 8, 2013

    Hello, I’m trying to use your codes as a guide to have certain ads banner displayed on desktop only. This is what I have, which I pasted in header.php where I want to display the ad:

    function detect_mobile()
    {
    if(preg_match(‘/(alcatel|amoi|android|avantgo|blackberry|benq|cell|cricket|docomo|elaine|htc|iemobile|iphone|ipaq|ipod|j2me|java|midp|mini|mmp|mobi|motorola|nec-|nokia|palm|panasonic|philips|phone|playbook|sagem|sharp|sie-|silk|smartphone|sony|symbian|t-mobile|telus|up\.browser|up\.link|vodafone|wap|webos|wireless|xda|xoom|zte)/i’, $_SERVER['HTTP_USER_AGENT']))
    return ”;

    else
    return ”;
    }

    What am i doing wrong?

    Reply
  8. Rick
    September 8, 2013

    Hello, I’m trying to use your codes as a guide to have certain ads banner displayed on desktop only. This is what I have, which I pasted in header.php where I want to display the ad:

    function detect_mobile()
    {
    if(preg_match(‘/(alcatel|amoi|android|avantgo|blackberry|benq|cell|cricket|docomo|elaine|htc|iemobile|iphone|ipaq|ipod|j2me|java|midp|mini|mmp|mobi|motorola|nec-|nokia|palm|panasonic|philips|phone|playbook|sagem|sharp|sie-|silk|smartphone|sony|symbian|t-mobile|telus|up\.browser|up\.link|vodafone|wap|webos|wireless|xda|xoom|zte)/i’, $_SERVER['HTTP_USER_AGENT']))
    return ”;

    else
    return ”;
    }

    What am i doing wrong?

    Reply
  9. Rick
    September 8, 2013

    looks like its stripping the codes i wrote on the return lines.

    but it’s a code

    Reply
  10. الدفاع
    October 20, 2013

    Excellent work.

    Anyone know what to put in for blackberry? The other mobile users are having no issue but can’t get the right agent for blackberry.

    Reply
  11. Diana Morgan
    November 23, 2013

    Wow, incredible blog layout! The overall look of your site is fantastic, let alone the content!

    Reply

Leave a Reply