Reputation: 12750
What is the best way to detect if user is a mobile user in code level?
Admin can set regex based rules to detect and switch themes based on user agent but seems like this functionality is "protected" inside api and is not available as a set of methods every developer can use.
I understand that devs and designers should arrange their functionality in theme folders to be available/unavailable but this is rather hard requirement for extension providers as the regex rules can be somewhat loosely defined and admin does not have to specify if a theme is intended for mobile users
So maybe someone has a good method to sniff this out from core already (without implementing stuff over again to be public instead protected that is already available core/design_package
)?
Upvotes: 6
Views: 15976
Reputation: 264
You can try next way:
Mage::run($mageRunCode, $mageRunType);
then use condition like this
if (is_mobile()) { Mage::run('mobile_store_code'); } else { Mage::run($mageRunCode, $mageRunType); }
Just as variant.
UPD: for example decet method it self:
function is_mobile() {
$user_agent=strtolower(getenv('HTTP_USER_AGENT'));
$accept=strtolower(getenv('HTTP_ACCEPT'));
if ((strpos($accept,'text/vnd.wap.wml')!==false) ||
(strpos($accept,'application/vnd.wap.xhtml+xml')!==false)) {
return 1;
}
if (isset($_SERVER['HTTP_X_WAP_PROFILE']) ||
isset($_SERVER['HTTP_PROFILE'])) {
return 2;
}
return 0;
}
Code taken from http://www.manhunter.ru/webmaster/272_opredelenie_mobilnih_brauzerov_na_php.html
Or this one
function isMobile()
{
$regex_match = "/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|"
. "htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|"
. "blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|"
. "symbian|smartphone|mmp|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|"
. "jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220"
. ")/i";
if (preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']))) {
return TRUE;
}
if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
return TRUE;
}
$mobile_ua = strtolower(substr($_SERVER['HTTP_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)) {
return TRUE;
}
if (isset($_SERVER['ALL_HTTP']) && strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini') > 0) {
return TRUE;
}
return FALSE;
}
Code taken from http://snippy.ru/snippet/1864-Prostoy_sposob_opredelit_zahod_na_stranicu_cherez_mobilnyy_brauzer/
There many such example can be found with Google ;)
Upvotes: 5
Reputation: 69621
How about this? Seems to work pretty well for me.
$isMobile = Zend_Http_UserAgent_Mobile::match(
Mage::helper('core/http')->getHttpUserAgent(),
$_SERVER
);
Though I haven't done enough research, maybe the wurfl adapter is more robust, per this thread.
Upvotes: 16