17

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

ItalyPaleAle
  • 7,185
  • 6
  • 42
  • 69

7 Answers7

31

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'] );
Floern
  • 33,559
  • 24
  • 104
  • 119
  • I like this code. Clean and simple! Thank you! What if I wanted to add iPad and all other tablets? I know they've got bigger screens, but I need them to be considered as mobile. For the iPad, I think I need to replace ip(hone|od) with ip(hone|od|ad), but what for android tablets and others? (TouchPad and PlayBook?) – ItalyPaleAle Jul 11 '11 at 10:02
  • 1
    Thanks this works well. I did find an issue with the Samsung S3350 though, the HTTP_USER_AGENT returns( Mozilla/5.0 (compatible; OpenWeb 5.7.2-10) Opera 8.54) My regex skills are little wanting, so have not tried to add to your expression. Any takers? – Sydwell Feb 07 '12 at 18:11
12

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.

ItalyPaleAle
  • 7,185
  • 6
  • 42
  • 69
Șerban Ghiță
  • 1,899
  • 20
  • 21
  • Love this script - updated on a regular basis and has worked great for us so far. I even found a rare tablet that wasn't being counted as tablet, and the creator had it added within a couple days. Good stuff. – Midnightly Coder Jul 02 '14 at 13:50
1

Floern Thanks for the code!!!

I added the Hp TouchPad Tablet with a:

hpwos

Here it is:

$isMobile = (bool)preg_match('#\b(ip(hone|od|ad)|android|opera m(ob|in)i|windows (phone|ce)|blackberry||hpwos|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'] );

Dominicanstan
  • 205
  • 1
  • 5
  • 7
0

You can use PHP's built-in function get_browser(). According to the PHP Manual the output array has platform field which you may use to detect mobile browsers.

There is also a lightweight mobile device detection code by Mobiforge.

Emre Yazici
  • 10,136
  • 6
  • 48
  • 55
0

If you don't need advanced capabilities detection, you could use this resource. I think new WURFL apis are AGPL, so they are free. Anyway you could use old version, is't?

fravelgue
  • 2,633
  • 2
  • 24
  • 23
  • It's free to download and redistribute, but not free to use in commercial projects, unless you release the source code (and obviously we will never do that). A license cost 1,500 USD! – ItalyPaleAle Jul 11 '11 at 10:00
  • Ok, sorry i thought free like open source – fravelgue Jul 11 '11 at 11:39
  • To clarify for posterity: WURFL (as referenced in the original question) is the one with the commercial license. The link to detectmobilebrowser.com in this particular answer is "free and unencumbered software released into the public domain." – Funka Apr 25 '12 at 22:10
-1

Two other ideas are:

  1. Some of the major carriers insert their own header: you can use this if you care, as long as users are through the carrier network.
  2. You can use JavaScript to detect the screen size. Lots of companies are tring to implement Responsive Design by doing tiny web pages: so, this is maybe a better, simpler approach. Also, HTML5 has media-queries to change css based on screen size.
ItalyPaleAle
  • 7,185
  • 6
  • 42
  • 69
maxweber
  • 576
  • 1
  • 5
  • 12
  • The #1 idea is flawed, as you cannot just rely on users being on cellular network. And not all carriers insert the header! The #2 can't work as well, because a desktop web browser may have a reduced window and - mostly important - I asked for a server-side solution :) – ItalyPaleAle Oct 26 '12 at 00:07
  • It is correct that not all carriers insert headers Qualcuno. But you can in fact check the ones that do. As to your second claim, you can communicate from client to server. In fact, User-Agent comes from client to server. I was suggesting to you some alternatives since you already have the other good suggestions. In fact, what do you think media-queries do on a desktop browser when the window size was reduced by the user? They exactly do as you think "can't work": they adjust the "responsive design" to the window width - probably not what the user wants. E.g. user comparing windows side-by-sid – maxweber Nov 15 '12 at 19:29
-2

Sorry I mis-read your question before

There is a very easy way to detect if someone is using a mobile or PC

This is what I do

IN my form display results I add the following at the VERY end of the form data capture;

    $msg .= "Referers: ".$_POST['Referers']. "\r\n";

Then Just Under the field I add this

  [[input type="hidden"  id="Referers" name="Referers" value="[[?php 
  echo "IP: " . $_SERVER['REMOTE_ADDR']; 
  echo "\nURL: " . $_SERVER['HTTP_REFERER']; 

echo "\nWebsite: " . $URL;
echo "\n\nBrowser Type:" . $_SERVER['HTTP_USER_AGENT'];
?]]"]]

Note that I changed the

The results PRINT out like this on my emails::

Referrers: IP: xxx.xxx.xxx.xxx // I am hiding this

URL: http://www.emailmarketingaustralia.com.au/Contact/index.php Website: http://www.emailmarketingaustralia.com.au

Browser Type:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; HTC_Sensation_Z710e; en-in) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16

What is interesting is that I am in AUstralia, yet the GEO-REGION lists me as en-in (India)

// GEO CITY

You can also get data from MAX MIND (www.maxmind.com) if you want the IP to display the city and country

This is simple and clean and gives you want you need