Notice: Undefined index: HTTP_ACCEPT in /var/local/cache/midgard/midgard/31-100-217-0.php(66) : eval()'d code on line 11

Blog: Archive

2009-04-01 - 2009-04-30

Mobilephone browser detecting from PHP

Posted on 2009-04-17 07:07:34 UTC to .

While ago friend of me asked for a script to detect if site is browsed with mobile device.
There are lot of these around on the net and here is my contribution to that list.

Hopefully someone else find this usefull also:

 

public function is_mobile($include_iphone_and_ipod=true)
{
$mobile_browser = 0;
$user_agent = $this->get_value('_SERVER', 'HTTP_USER_AGENT');

if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($user_agent))) {
$mobile_browser++;
}

if (! $include_iphone_and_ipod) {
if (preg_match('/(iphone|ipod)/i', strtolower($user_agent))) {
$mobile_browser--;
}
}

if ( (strpos(strtolower($this->get_value('_SERVER', 'HTTP_ACCEPT')),'application/vnd.wap.xhtml+xml')>0)
|| (( $this->has_value('_SERVER', 'HTTP_X_WAP_PROFILE')
|| $this->has_value('_SERVER', 'HTTP_PROFILE'))))
{
$mobile_browser++;
}

$mobile_ua = strtolower(substr($user_agent, 0, 4));
$mobile_agents = array(
'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
'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','oper','palm','pana','pant','phil','play','port','prox',
'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','webc','winw','winw','xda','xda-'
);

if (in_array($mobile_ua, $mobile_agents)) {
$mobile_browser++;
}
if ( $this->has_value('_SERVER', 'ALL_HTTP')
&& strpos(strtolower($this->get_value('_SERVER', 'ALL_HTTP')), 'OperaMini') > 0)
{
$mobile_browser++;
}

if (strpos(strtolower($user_agent), 'windows') > 0) {
$mobile_browser = 0;
}

if ($mobile_browser > 0) {
return true;
}

return false;
}

Back