Posted By

ghprod on 04/08/12


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Malm
Hero


PHP Mobile Detector


 / Published in: PHP
 

PHP Mobile Detector

  1. <?php
  2.  
  3. /*
  4.  * Lightweight detector of mobile devices, OSs & browsers
  5.  * Copyright 2012 Túbal Martín (email: [email protected])
  6.  * License: GPL2
  7.  */
  8.  
  9. if ( ! function_exists('mobile_detector') )
  10. {
  11. // Global vars
  12. $is_mobile = false;
  13. $is_iphone = $is_ipad = $is_kindle = false;
  14. $is_ios = $is_android = $is_webos = $is_palmos = $is_windows = $is_symbian = $is_bbos = $is_bada = false;
  15. $is_opera_mobile = $is_webkit_mobile = $is_firefox_mobile = $is_ie_mobile = $is_netfront = $is_uc_browser = false;
  16.  
  17.  
  18. function mobile_detector($debug = false)
  19. {
  20. global $is_mobile;
  21.  
  22. // Check user agent string
  23. $agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
  24.  
  25. if (empty($agent)) {
  26. return;
  27. }
  28.  
  29. $mobile_devices = array(
  30. 'is_iphone' => 'iphone',
  31. 'is_ipad' => 'ipad',
  32. 'is_kindle' => 'kindle'
  33. );
  34.  
  35. $mobile_oss = array(
  36. 'is_ios' => 'ip(hone|ad|od)',
  37. 'is_android' => 'android',
  38. 'is_webos' => '(web|hpw)os',
  39. 'is_palmos' => 'palm(\s?os|source)',
  40. 'is_windows' => 'windows (phone|ce)',
  41. 'is_symbian' => 'symbian(\s?os|)|symbos',
  42. 'is_bbos' => 'blackberry(.*?version\/\d+|\d+\/\d+)',
  43. 'is_bada' => 'bada'
  44. );
  45.  
  46. $mobile_browsers = array(
  47. 'is_opera_mobile' => 'opera (mobi|mini)', // Opera Mobile or Mini
  48. 'is_webkit_mobile' => '(android|nokia|webos|hpwos|blackberry).*?webkit|webkit.*?(mobile|kindle|bolt|skyfire|dolfin|iris)', // Webkit mobile
  49. 'is_firefox_mobile' => 'fennec', // Firefox mobile
  50. 'is_ie_mobile' => 'iemobile|windows ce', // IE mobile
  51. 'is_netfront' => 'netfront|kindle|psp|blazer|jasmine', // Netfront
  52. 'is_uc_browser' => 'ucweb' // UC browser
  53. );
  54.  
  55. $groups = array($mobile_devices, $mobile_oss, $mobile_browsers);
  56.  
  57. foreach ($groups as $group) {
  58. foreach ($group as $name => $regex) {
  59. if (preg_match('/'.$regex.'/i', $agent)) {
  60. global $$name;
  61. $is_mobile = $$name = true;
  62. break;
  63. }
  64. }
  65. }
  66.  
  67. // Fallbacks
  68. if ($is_mobile === false) {
  69. $regex = 'nokia|motorola|sony|ericsson|lge?(-|;|\/|\s)|htc|samsung|asus|mobile|phone|tablet|pocket|wap|wireless|up\.browser|up\.link|j2me|midp|cldc|kddi|mmp|obigo|novarra|teleca|openwave|uzardweb|pre\/|hiptop|avantgo|plucker|xiino|elaine|vodafone|sprint|o2';
  70. $accept = isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : '';
  71.  
  72. if (false !== strpos($accept,'text/vnd.wap.wml')
  73. || false !== strpos($accept,'application/vnd.wap.xhtml+xml')
  74. || isset($_SERVER['HTTP_X_WAP_PROFILE'])
  75. || isset($_SERVER['HTTP_PROFILE'])
  76. || preg_match('/'.$regex.'/i', $agent)
  77. ) {
  78. $is_mobile = true;
  79. }
  80. }
  81.  
  82. // DEBUGGER OUTPUT
  83. if ($debug === true) {
  84. echo '<strong>User Agent: '.$agent.'</strong><br>';
  85. foreach ($GLOBALS as $k => $v) {
  86. if (strpos($k, 'is_') !== false) {
  87. echo '<span style="color:'.($v ? 'green':'red').';">$'.$k.'</span><br>';
  88. }
  89. }
  90. }
  91.  
  92. }
  93.  
  94. // execute inmmediatly
  95. mobile_detector();
  96.  
  97. }

Report this snippet  

You need to login to post a comment.