free hit counter

Check If The Request Came From Mobile Or Computer In PHP 1

Check If The Request Came From Mobile Or Computer In PHP

Check If The Request Came From Mobile Or Computer In PHP

*In today’s world it is important to make your website accessible to all of your users. Although many people still access your website though their computer, a huge amount of people are also accessing your website from their phones and tablets. When you are programming your website it is important to keep these types of media in mind so that your site will work on these devices.

One good thing to know is that PHP pages work on all of these devices! The reason is that PHP is all processed on the server, so by the time the code gets to the user, it is just HTML. So basically, the user requests a page of your website from your server, your server then runs all the PHP and sends the user the results of the PHP. The device never actually sees or has to do anything with the actual PHP code. This gives websites done in PHP an advantage over other languages that process on the user side, such as Flash.

It has become popular to redirect users to mobile versions of your website. This is something that you can do with the htaccess file but you can also do with PHP. ( * )

You need to check if the request came from a mobile phone device or desktop computer , use this function

CakePHP Method

  1. if ($this->RequestHandler->isMobile()) {   
  2. /*  condition for mobile device  */  
  3. }  
  4. else {  
  5. /*  condition for PC  */  
  6. }  

Core PHP Method

  1. function ismobile() {  
  2.     $is_mobile = ‘0’;  
  4.     if(preg_match(‘/(android|up.browser||mmp|symbian|smartphone|midp|wap|phone)/i’strtolower($_SERVER[‘HTTP_USER_AGENT’]))) {  
  5.         $is_mobile=1;  
  6.     }  
  8.     if((strpos(strtolower($_SERVER[‘HTTP_ACCEPT’]),‘application/vnd.wap.xhtml+xml’)>0) or ((isset($_SERVER[‘HTTP_X_WAP_PROFILE’]) or isset($_SERVER[‘HTTP_PROFILE’])))) {  
  9.         $is_mobile=1;  
  10.     }  
  12.     $mobile_ua = strtolower(substr($_SERVER[‘HTTP_USER_AGENT’],0,4));  
  13.     $mobile_agents = array(‘w3c ‘,‘acs-‘,‘alav’,‘alca’,‘amoi’,‘andr’,‘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-‘);  
  15.     if(in_array($mobile_ua,$mobile_agents)) {  
  16.         $is_mobile=1;  
  17.     }  
  19.     if (isset($_SERVER[‘ALL_HTTP’])) {  
  20.         if (strpos(strtolower($_SERVER[‘ALL_HTTP’]),‘OperaMini’)>0) {  
  21.             $is_mobile=1;  
  22.         }  
  23.     }  
  25.     if (strpos(strtolower($_SERVER[‘HTTP_USER_AGENT’]),‘windows’)>0) {  
  26.         $is_mobile=0;  
  27.     }  
  29.     return $is_mobile;  
  30. }  

One comment on “Check If The Request Came From Mobile Or Computer In PHP

  1. Reply domain Oct 6,2014 2:00 pm

    Hello there I am so thrilled I found your weblog, I really found you by accident, while I
    was searching on Digg for something else, Anyways I am here now and would just like to say thanks for a incredible
    post and a all round thrilling blog (I also love
    the theme/design), I don’t have time to read through
    it all at the minute but I have bookmarked it and also included your RSS feeds, so when I have time
    I will be back to read a lot more, Please do keep up the
    awesome work.

Leave a Reply to domain Cancel Reply