Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nerveosu

macrumors member
Original poster
Sep 17, 2001
85
0
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'];
}

?>
 
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.
 
wouldn't
Code:
in_array($_SERVER['HTTP_USER_AGENT'], $useragents)
make more sense than a for loop?
 
re

Even if this topic is a bit old, the mobile detection problem is rather new. Try Mobile_Detect class from http://code.google.com/p/php-mobile-detect/

Code:
Mobile_Detect is a lightweight PHP class for detecting mobile devices. It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.