Wednesday, August 31, 2011

Detecting mobile browsers in PHP

I was recently trying to find some code to detect if a visitor to my website was coming from a mobile browser so I could give a mobile friendly stylesheet, and after quite a bit of searching couldn't find anything for PHP.

I was able to find lists of mobile client user agent strings, but there are hundreds, and you don't want to be checking against hundreds of strings for every request, as it is slightly time consuming. After a bit more searching I came across this website, which gives a python algorithm for detecting mobile browsers.

The website gives a much smarter approach than checking every possible user agent. It checks for the most common mobile browsers, and if it doesn't find a match, checks against common desktop operating systems and web crawler user agents. If it still doesn't have a match it just assumes that it's a mobile browser. I used this algorithm and put together the following PHP code:

<?php

function is_mobile($user_agent, $default_to_mobile = true) {
   // Common mobile browsers
   $mobile_regex = "/iphone|ipod|blackberry|android|palm|windows ce|fennec/i";
   if (preg_match($mobile_regex, $user_agent) === 1)
      return true;

   // Desktop and bots
   $desktop_regex = "/windows|linux|os x|solaris|bsd|spider|crawl|slurp|bot/i";
   if (preg_match($desktop_regex, $user_agent) === 1)
      return false;
   
   // Assume it's an uncommon mobile browser
   // unless $default_to_mobile is false
   return $default_to_mobile;
}
   
?>

So all that is needed to be done once the above code is included, is the following:

if (is_mobile($_SERVER['HTTP_USER_AGENT'])) {
   // use mobile template and stylesheet
} else {
   // use desktop template and stylesheet
}

Hopefully this saves you the time to track down an algorithm and write the code for it. Let me know if you find this useful or can suggest any improvements.

Until next time, peace.

15 comments:

  1. Hi,

    Mobile browsers also called micro browsers, and mini browsers are web browsers designed for use on handheld multimedia devices such as mobile phones and PDAs. They are typically stripped-down versions of standard web browsers and optimized for low bandwidth and a small display screen. Thanks a lot...

    Mobile Application Analytics

    ReplyDelete
    Replies
    1. Great Article android based projects

      Java Training in Chennai

      Project Center in Chennai

      Java Training in Chennai

      projects for cse

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. Thanks for sharing your script.

    The DetectMobileBrowsers.com open source project uses essentially this approach but provides a much longer regex. You can download it in PHP and almost any other language imaginable here:
    http://detectmobilebrowsers.com/

    ReplyDelete
  3. Mobile_Detect is a compact PHP class for discovering cellular phone gadgets. It uses the user-agent chain along with specific HTTP headers to identify the cellular environment.

    ReplyDelete
  4. In the Modern era integrating Artificial Technology in the E-commerce store is an effective ways to increase the customer engagement with the website. The Blog is very informative and useful.
    Hire Dedicated Developers
    Hire Cake Php Developer
    Smarty Developers
    Hire Cake Php Programmer
    Hire Phonegap Developer

    ReplyDelete
  5. Artificial Intelligence is the latest technology that transforms the way the E-commerce store performs. Thanks for sharing an informative blog.
    Hire Wordpress Developer
    Hire Magento Developer
    Hire Web Developer
    Hire Wordpress Programmer
    Hire PHP Developer India

    ReplyDelete
  6. Such a great blog and all the writings. Can’t help thinking about the author.
    spy phone

    ReplyDelete
  7. This is acceptable to know in this season of ecological mindfulness.Iphone reparatur

    ReplyDelete
  8. Hire Dedicated PHP Developer from professional organizations helps you to get some benefits from them, but it is not always the scenario that you get the best developers from them.

    ReplyDelete
  9. I will truly value the essayist's decision for picking this magnificent article fitting to my matter.Here is profound depiction about the article matter which helped me more.
    data analytics course

    ReplyDelete
  10. I think I have never watched such online diaries ever that has absolute things with all nuances which I need. So thoughtfully update this ever for us.
    difference between analysis and analytics

    ReplyDelete
  11. Stunning! Such an astonishing and supportive post this is. I incredibly love it. It's so acceptable thus wonderful. I am simply astounded.
    data science training

    ReplyDelete
  12. Hire PHP Developer as they know it very well that PHP is faster as compared to prevailing scripting languages.

    ReplyDelete
  13. You finished certain solid focuses there. I did a pursuit regarding the matter and discovered almost all people will concur with your blog.
    iot course in noida

    ReplyDelete
  14. A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.
    business analytics course

    ReplyDelete