best way to detect mobile users in magento

best way to detect mobile users in magento  using -'magento'

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)?

asked Oct 23, 2015 by mca.agarwal
0 votes

2 Answers

0 votes

You can try next way:

  • Create new store for your website with own store view which configure to use desired mobile theme package
  • go to index.php and add there some code to check is current user use mobile device (this question was already there how to check if the request came from mobile or computer in php )
  • add this check BEFORE line

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() {

  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

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',  
        '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

There many such example can be found with Google ;)

answered Oct 23, 2015 by ashish singh
0 votes

How about this? Seems to work pretty well for me.

$isMobile = Zend_Http_UserAgent_Mobile::match(

Though I haven't done enough research, maybe the wurfl adapter is more robust, per this thread.

answered Oct 23, 2015 by shegokar.anjeet