PDA

View Full Version : Here is how to detect a mobile device using php.




nerveosu
May 31, 2006, 01:36 PM
For anyone who wants to use it..

This is a way to detect with php if a visitor to your web site is using a mobile device browser.. it works. If you have a mobile device that this doesn't detect, all you have to do to add it is to find a unique bit of identifying information in your device's user agent and stick it in the list of mobile devices in this code.

------------

<?php


/* detect mobile device*/
$ismobile = 0;
$container = $_SERVER['HTTP_USER_AGENT'];

// A list of mobile devices
$useragents = array (

'Blazer' ,
'Palm' ,
'Handspring' ,
'Nokia' ,
'Kyocera',
'Samsung' ,
'Motorola' ,
'Smartphone',
'Windows CE' ,
'Blackberry' ,
'WAP' ,
'SonyEricsson',
'PlayStation Portable',
'LG',
'MMP',
'OPWV',
'Symbian',
'EPOC',

);

foreach ( $useragents as $useragents ) {
if(strstr($container,$useragents)) {
$ismobile = 1;
}
}



if ( $ismobile == 1 ) {
echo "<p>mobile device</p>";
echo $_SERVER['HTTP_USER_AGENT'];
}

?>



beradrian
Oct 10, 2008, 11:25 AM
You can see here a few other (better) methods to detect a mobile device: http://beradrian.wordpress.com/2008/10/10/mobile-device-recognition/

angelwatt
Oct 10, 2008, 12:14 PM
You can see here a few other (better) methods to detect a mobile device: http://beradrian.wordpress.com/2008/10/10/mobile-device-recognition/

How is that better? Looks more complicated to me.

qwertydesign
Nov 18, 2008, 05:58 PM
there is also a web service out there that provides GEO location as well as handset information real time.

There are a few out there that do this, but we use handsetdetection.com when developing new sites for clients.

web_god61
Nov 18, 2008, 06:36 PM
wouldn't in_array($_SERVER['HTTP_USER_AGENT'], $useragents) make more sense than a for loop?