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.
Hi,
ReplyDeleteMobile 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
Great Article android based projects
DeleteJava 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
Thanks for sharing your script.
ReplyDeleteThe 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/
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.
ReplyDeleteIn 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.
ReplyDeleteHire Dedicated Developers
Hire Cake Php Developer
Smarty Developers
Hire Cake Php Programmer
Hire Phonegap Developer
Artificial Intelligence is the latest technology that transforms the way the E-commerce store performs. Thanks for sharing an informative blog.
ReplyDeleteHire Wordpress Developer
Hire Magento Developer
Hire Web Developer
Hire Wordpress Programmer
Hire PHP Developer India
Such a great blog and all the writings. Can’t help thinking about the author.
ReplyDeletespy phone
This is acceptable to know in this season of ecological mindfulness.Iphone reparatur
ReplyDeleteHire 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.
ReplyDeleteI 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.
ReplyDeletedata analytics course
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.
ReplyDeletedifference between analysis and analytics
Stunning! Such an astonishing and supportive post this is. I incredibly love it. It's so acceptable thus wonderful. I am simply astounded.
ReplyDeletedata science training
Hire PHP Developer as they know it very well that PHP is faster as compared to prevailing scripting languages.
ReplyDeleteYou finished certain solid focuses there. I did a pursuit regarding the matter and discovered almost all people will concur with your blog.
ReplyDeleteiot course in noida
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.
ReplyDeletebusiness analytics course