现在的位置: 首页 > 综合 > 正文

[PHP]如何使用Mobile_Detect来判断访问网站的设备:安卓,平板,电脑

2013年02月16日 ⁄ 综合 ⁄ 共 3568字 ⁄ 字号 评论关闭

转载:http://blog.csdn.net/pleasecallmewhy/article/details/9981453

Mobile_Detect 是一个轻量级的开源移动设备(手机)检测的 PHP Class,

它使用 User-Agent 中的字符串,并结合 HTTP Header,来检测移动设备环境。

这个设备检测的 PHP 类库最强大的地方是,它有一个非常完整的库,

可以检测出所用的设备类型(包括操作类型,以及手机品牌等都能检测)和浏览器的详细信息。

完整的Mobile_Detect代码如下:

  1. <?php  
  2. /** 
  3.  * MIT License 
  4.  * =========== 
  5.  * 
  6.  * Permission is hereby granted, free of charge, to any person obtaining 
  7.  * a copy of this software and associated documentation files (the 
  8.  * "Software"), to deal in the Software without restriction, including 
  9.  * without limitation the rights to use, copy, modify, merge, publish, 
  10.  * distribute, sublicense, and/or sell copies of the Software, and to 
  11.  * permit persons to whom the Software is furnished to do so, subject to 
  12.  * the following conditions: 
  13.  * 
  14.  * The above copyright notice and this permission notice shall be included 
  15.  * in all copies or substantial portions of the Software. 
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
  18.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
  19.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
  20.  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
  21.  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
  22.  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
  23.  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
  24.  * 
  25.  * 
  26.  * @author      Serban Ghita <serbanghita@gmail.com> 
  27.  *              Victor Stanciu <vic.stanciu@gmail.com> (until v. 1.0) 
  28.  * @license     MIT License https://github.com/serbanghita/Mobile-Detect/blob/master/LICENSE.txt 
  29.  * @link        Official page: http://mobiledetect.net 
  30.  *              GitHub Repository: https://github.com/serbanghita/Mobile-Detect 
  31.  *              Google Code Old Page: http://code.google.com/p/php-mobile-detect/ 
  32.  * @version     2.6.6 
  33.  */  
  34.   
  35. class Mobile_Detect  
  36. {  
  37.     /** 
  38.      * Mobile detection type. 
  39.      */  
  40.     const DETECTION_TYPE_MOBILE     = 'mobile';  
  41.   
  42.     /** 
  43.      * Extended detection type. 
  44.      */  
  45.     const DETECTION_TYPE_EXTENDED   = 'extended';  
  46.   
  47.     /** 
  48.      * A frequently used regular expression to extract version #s. 
  49.      */  
  50.     const VER                       = '([\w._\+]+)';  
  51.   
  52.     /** 
  53.      * Top-level device. 
  54.      */  
  55.     const MOBILE_GRADE_A            = 'A';  
  56.   
  57.     /** 
  58.      * Mid-level device. 
  59.      */  
  60.     const MOBILE_GRADE_B            = 'B';  
  61.   
  62.     /** 
  63.      * Low-level device. 
  64.      */  
  65.     const MOBILE_GRADE_C            = 'C';  
  66.   
  67.     /** 
  68.      * Stores the version number of the current release. 
  69.      * @var array 
  70.      */  
  71.     protected $scriptVersion = '2.6.6';  
  72.   
  73.     /** 
  74.      * The User-Agent HTTP header is stored in here. 
  75.      * @var string 
  76.      */  
  77.     protected $userAgent = null;  
  78.   
  79.     /** 
  80.      * HTTP headers in the PHP-flavor. So HTTP_USER_AGENT and SERVER_SOFTWARE. 
  81.      * @var array 
  82.      */  
  83.     protected $httpHeaders = array();  
  84.   
  85.     /** 
  86.      * All the rules combined. 
  87.      * @var array 
  88.      */  
  89.     protected $mobileDetectionRules = null;  
  90.   
  91.     /** 
  92.      * The rules, but extended. 
  93.      * @var array 
  94.      */  
  95.     protected $mobileDetectionRulesExtended = null;  
  96.   
  97.     /** 
  98.      * The detection type, using self::DETECTION_TYPE_MOBILE or self::DETECTION_TYPE_EXTENDED. 
  99.      * 
  100.      * @var string 
  101.      */  
  102.     protected $detectionType = self::DETECTION_TYPE_MOBILE;  
  103.   
  104.     /** 
  105.      * List of mobile devices (phones). 
  106.      * 
  107.      * @var array 
  108.      */  
  109.     protected $phoneDevices = array(  
  110.         'iPhone'        => '\biPhone.*Mobile|\biPod'

抱歉!评论已关闭.