PHP CODE TO DETECT MOBILE BROWSERS
Detecting Handheld devices like , mobile phones, PDA, WAP enabled devices, WAP enabled Browsers is possible with PHP Scripting.
The code that detects mobile devices:
$wapprofile=$_SERVER['HTTP_X_WAP_PROFILE']; // mobile profile
$httpprofile=$_SERVER['HTTP_PROFILE']; // http profile
$browser=$_SERVER['HTTP_USER_AGENT']; // user-agent info
// — MOBILE DETECTION — //
$wml=0;
$xhtml=0;
if(preg_match(‘/(midp|wap|up.browser|up.link|mmp|symbian|smartphone|phone)/i’,strtolower($browser))) {
$wml++;
}
if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),’application/vnd.wap.xhtml+xml’)>0)) {
$xhtml++;
$wml++;
}
if(strpos(strtolower($_SERVER['HTTP_ACCEPT']),’xhtml+xml’)>0) {
$xhtml++;
}
if(isset($wapprofile) or isset($httpprofile)) {
$wml++;
}
$mobile_ua = strtolower(substr($browser,0,4));
$mobile_agents = array(
‘doco’,'eric’,'hipt’,'inno’,'w3c ‘,’audi’,'avan’,'benq’,'bird’,'blac’,
‘blaz’,'brew’,'cell’,'oper’,'palm’,'pana’,'pant’,'cldc’,'cmd-’,'dang’,
‘ipaq’,'java’,'jigs’,'kddi’,'keji’,'leno’,'lg-c’,'lg-d’,'lg-g’,'lge-’,
‘maui’,'maxo’,'midp’,'mits’,'mmef’,'mobi’,'mot-’,'moto’,'mwbp’,'nec-’,
‘newt’,'noki’,'phil’,'play’,'port’,'prox’,'acs-’,'alav’,'alca’,'amoi’,
‘qwap’,’sage’,’sams’,’sany’,’sch-’,’sec-’,’send’,’seri’,’sgh-’,’shar’,
’sie-’,’siem’,’smal’,’smar’,’sony’,’sph-’,’symb’,'t-mo’,'teli’,'tim-’,
‘tosh’,'tsm-’,'upg1′,’upsi’,'vk-v’,'voda’,'wap-’,'wapa’,'wapi’,'wapp’,
‘wapr’,'xda-’,'webc’,'winw’,'winw’,'xda’);
if(in_array($mobile_ua,$mobile_agents)){
$wml++;
}
if(strpos(strtolower($_SERVER['ALL_HTTP']),’OperaMini’)>0) {
$xhtml++;
$wml++;
}
if(strpos(strtolower($browser),’windows’)>0) {
$xhtml++;
}
if($xhtml && $wml)
echo “Supports both XHTML and WML codes – Mobie with XHTML”;
else if($wml && ($xhtml==0))
echo “Supports WML only – truely mobile”;
else if(($wml==0) && $xhtml)
echo “Supports XHTML (ex: Desktop Browser) – may be mobile”;
else
echo “Not a Mobile”;
- You should replace the appropriate lines for Redirecting to web pages to a particular devices.
Popularity: 39%

October 15th, 2008 at 4:09 pm
A Simple code solves my problem in detecting Mobile devices. Lots of thanks to you.