Reputation: 7334
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
Upvotes: 17
Views: 17634
Reputation: 576
Two other ideas are:
Upvotes: -1
Reputation: 9
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
Upvotes: -2
Reputation: 205
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'] );
Upvotes: 1
Reputation: 33904
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'] );
Upvotes: 32
Reputation: 10174
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.
Upvotes: 0