Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mobile browser detection?

We are looking for a way to determine if a user is using a mobile browser. We need to do that in PHP, by parsing the user agent string. I know this method has got many caveats, but we really need to do it that way.

Do you have any suggestion? A good (even if not perfect) updated code?

I know about WURFL, and I believe it's great, but it's not free to use anymore for non open source projects. By googling a bit, I also found this code: http://mobiforge.com/developing/story/lightweight-device-detection-php (and some variations), but I'm not sure about it. Looks like it's written really bad (look, for example, when they use $mobile_browser = '0' with the quotes around an integer...).

Can you recommend something?

Thank you,

Alessandro

like image 612
ItalyPaleAle Avatar asked Jul 09 '11 17:07

ItalyPaleAle


People also ask

How do websites detect mobile?

In most cases websites will look at the user agent header and then redirect or serve mobile content if accessed from mobile device. However, there are some sites that will serve mobile content even if you change agent in your browser (request desktop site in Android, for example).

Is mobile-detect JS?

Mobile-Detect A lightweight PHP class for detecting mobile devices (including tablets). This is the “source” of this JavaScript project and if you use PHP on your server you should use it!


2 Answers

I am using this one:

$isMobile = (bool)preg_match('#\b(ip(hone|od)|android\b.+\bmobile|opera m(ob|in)i|windows (phone|ce)|blackberry'.
                    '|s(ymbian|eries60|amsung)|p(alm|rofile/midp|laystation portable)|nokia|fennec|htc[\-_]'.
                    '|up\.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\b#i', $_SERVER['HTTP_USER_AGENT'] );

It's short and does detect most mobile users (or rather smartphones). iPad and Android-Tablets won't be classified as 'mobile' since they have bigger screen sizes.

If you want to catch Tablets as well, you can use this:

$isMobile = (bool)preg_match('#\b(ip(hone|od|ad)|android|opera m(ob|in)i|windows (phone|ce)|blackberry|tablet'.
                    '|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp|laystation portable)|nokia|fennec|htc[\-_]'.
                    '|mobile|up\.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\b#i', $_SERVER['HTTP_USER_AGENT'] );
like image 157
Floern Avatar answered Sep 17 '22 15:09

Floern


I'm using Mobile_Detect class. It's updated almost weekly. The code looks for signs of 'mobile' device in both HTTP headers and User-Agent string.

Demo ← run this from your mobile device

You will probably be using only:

include("Mobile_Detect.php");
$detect = new Mobile_Detect();
if ($detect->isMobile()) {
    // any mobile platform
}

The class also attempts to detect tablet devices through:

if($detect->isTablet()){
    // any tablet
}

There is no silver bullet in detecting mobile devices, but this is a good start before deciding on using external APIs like WURLF, DeviceAtlas or others.

like image 32
Șerban Ghiță Avatar answered Sep 17 '22 15:09

Șerban Ghiță