How can I tell (in php) if a site is visited through a mobile device? I can't use get_browser() because I'm not able to edit php.ini (to set the path of browscap.ini).
Asked
Active
Viewed 2,527 times
1
-
There are many answers to this on SO like [this](https://stackoverflow.com/questions/6636306/mobile-browser-detection) or [this](https://stackoverflow.com/questions/3817155/php-mobile-browser-detection) – kero Jan 19 '14 at 14:02
-
2I dont like people down-voting questions.. So ill up vote you! – PriceCheaperton Jan 19 '14 at 14:03
-
1If you want to display a different site to mobile users, don't forget that the popular thing now is to use responsive web sites (you can search for it on Google). If you really want to detect through the user agent, I suggest that you download a code that already exists and that is maintained by somebody else. I used this one on some projects : http://detectmobilebrowsers.com/ The thing is that there is no way in the world that you can test the detection on all mobile devices because there are so many. And, when new mobile devices are sold, there a new user agents. – mogosselin Jan 19 '14 at 14:14
2 Answers
3
If PHP you can use the open source library called Mobile Detect
After download, you can include it inside your project and then use it like this:
// Include and instantiate the class.
include 'Mobile_Detect.php';
$detect = new Mobile_Detect();
if ($detect->isMobile())
// Any mobile device (phones or tablets).
else
// desktop detected
Alternative solution is to use client side technique such as javasascript by checking the user agent string:
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
// mobile device detected
}

Felix
- 37,892
- 8
- 43
- 55
1
I am using code from this site. It is in all modern languages and detects browsers and devices very efficiently.