Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday, September 11, 2012

Request Detection for Mobile Application

Market for mobile application is growing with a great speed. Even there are lots of mobile application created in PHP itself. Sometimes, we have common application for web and mobile but need to detect the mobile devices before rendering the content. Here is a useful code snippet to detect the source of request for application using PHP. If application is requested by any mobile device then the function will return true else it will return false.


    private function _isMobile()
    {
     if(isset($_SERVER["HTTP_X_WAP_PROFILE"])) return true;
     if(preg_match("/wap\.|\.wap/i",$_SERVER["HTTP_ACCEPT"])) return true;
     if(isset($_SERVER["HTTP_USER_AGENT"]))
     {
      // Quick Array to kill out matches in the user agent
      // that might cause false positives
      $badmatches = array("OfficeLiveConnector","MSIE\ 8\.0?,"OptimizedIE8?,"MSN\ Optimized","Creative\ AutoUpdate","Swapper");
      foreach($badmatches as $badstring){
       if(preg_match("/".$badstring."/i",$_SERVER["HTTP_USER_AGENT"])) return false;
      }
      // Now we’ll go for positive matches with the list of all possible types of request processors
      $uamatches = array("midp", "j2me", "avantg", "docomo", "novarra", "palmos", "palmsource", "240×320?, "opwv", "chtml", "pda", "windows\ ce", "mmp\/", "blackberry", "mib\/", "symbian", "wireless", "nokia", "hand", "mobi", "phone", "cdm", "up\.b", "audio", "SIE\-", "SEC\-", "samsung", "HTC", "mot\-", "mitsu", "sagem", "sony", "alcatel", "lg", "erics", "vx", "NEC", "philips", "mmm", "xx", "panasonic", "sharp", "wap", "sch", "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda", "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb","\d\d\di", "moto", "webos");
      foreach($uamatches as $uastring){
       if(preg_match("/".$uastring."/i",$_SERVER["HTTP_USER_AGENT"])) return true;
      }
     }
    return false;
    }

    if(!$this->_isMobile())
    {
     $this->layout='front';
    }
    else
    {
     $this->layout='mobile';
    }

No comments:

Post a Comment