Posted By

iTony on 08/04/08


Tagged

php detection mobile


Versions (?)

Who likes this?

12 people have marked this snippet as a favorite

wizard04
jamesming
mrjthethird
brent-man
keame
nb109
tannerc
ninjacharlee
dimispaceman
ljayz
sbcjr
tux-world


Detect device in mobile


 / Published in: PHP
 

  1. <?php
  2.  
  3. $mobile_browser = '0';
  4.  
  5. if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i',
  6. strtolower($_SERVER['HTTP_USER_AGENT']))){
  7. $mobile_browser++;
  8. }
  9.  
  10. if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or
  11. ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))){
  12. $mobile_browser++;
  13. }
  14.  
  15. $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
  16. $mobile_agents = array(
  17. 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
  18. 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
  19. 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
  20. 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
  21. 'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
  22. 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
  23. 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
  24. 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
  25. 'wapr','webc','winw','winw','xda','xda-');
  26.  
  27. if(in_array($mobile_ua,$mobile_agents)){
  28. $mobile_browser++;
  29. }
  30. if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) {
  31. $mobile_browser++;
  32. }
  33. if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {
  34. $mobile_browser=0;
  35. }
  36.  
  37.  
  38. if($mobile_browser>0){
  39. // do something
  40. } else {
  41. // do something else
  42. }
  43.  
  44.  
  45. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: wizard04 on August 4, 2008

I try to stay away from browser detection, but this could be quite useful. Was the user agent list updated recently?

You need to login to post a comment.