Dave's Brain

Browse - Programming Tips - Is there a modern/simple way to check for a mobile brower?

Date: 2014dec5
Language: php, perl

Q.  Is there a modern/simple way to check for a mobile brower?

A.

This page suggests just looking for "Mobi"
https://developer.mozilla.org/en-US/docs/Browser_detection_using_the_user_agent
And it makes sense to me.

In PHP:

function isMobile() { $useragent = $_SERVER['HTTP_USER_AGENT']; return strpos($useragent, 'Mobi') !== false; }
In Perl:
sub isMobile() { my $agent = $ENV{HTTP_USER_AGENT}; return $agent =~ m/Mobi/; }
In Java Servlet:
boolean isMobile(HttpServletRequest request) { final String agent = request.getHeader("User-Agent"); if (agent == null) return false; return agent.indexOf("Mobi") >= 0; }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.