转载:http://blog.csdn.net/pleasecallmewhy/article/details/9981453
Mobile_Detect 是一个轻量级的开源移动设备(手机)检测的 PHP Class,
它使用 User-Agent 中的字符串,并结合 HTTP Header,来检测移动设备环境。
这个设备检测的 PHP 类库最强大的地方是,它有一个非常完整的库,
可以检测出所用的设备类型(包括操作类型,以及手机品牌等都能检测)和浏览器的详细信息。
完整的Mobile_Detect代码如下:
- <?php
- /**
- * MIT License
- * ===========
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice shall be included
- * in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- *
- * @author Serban Ghita <serbanghita@gmail.com>
- * Victor Stanciu <vic.stanciu@gmail.com> (until v. 1.0)
- * @license MIT License https://github.com/serbanghita/Mobile-Detect/blob/master/LICENSE.txt
- * @link Official page: http://mobiledetect.net
- * GitHub Repository: https://github.com/serbanghita/Mobile-Detect
- * Google Code Old Page: http://code.google.com/p/php-mobile-detect/
- * @version 2.6.6
- */
- class Mobile_Detect
- {
- /**
- * Mobile detection type.
- */
- const DETECTION_TYPE_MOBILE = 'mobile';
- /**
- * Extended detection type.
- */
- const DETECTION_TYPE_EXTENDED = 'extended';
- /**
- * A frequently used regular expression to extract version #s.
- */
- const VER = '([\w._\+]+)';
- /**
- * Top-level device.
- */
- const MOBILE_GRADE_A = 'A';
- /**
- * Mid-level device.
- */
- const MOBILE_GRADE_B = 'B';
- /**
- * Low-level device.
- */
- const MOBILE_GRADE_C = 'C';
- /**
- * Stores the version number of the current release.
- * @var array
- */
- protected $scriptVersion = '2.6.6';
- /**
- * The User-Agent HTTP header is stored in here.
- * @var string
- */
- protected $userAgent = null;
- /**
- * HTTP headers in the PHP-flavor. So HTTP_USER_AGENT and SERVER_SOFTWARE.
- * @var array
- */
- protected $httpHeaders = array();
- /**
- * All the rules combined.
- * @var array
- */
- protected $mobileDetectionRules = null;
- /**
- * The rules, but extended.
- * @var array
- */
- protected $mobileDetectionRulesExtended = null;
- /**
- * The detection type, using self::DETECTION_TYPE_MOBILE or self::DETECTION_TYPE_EXTENDED.
- *
- * @var string
- */
- protected $detectionType = self::DETECTION_TYPE_MOBILE;
- /**
- * List of mobile devices (phones).
- *
- * @var array
- */
- protected $phoneDevices = array(
- 'iPhone' => '\biPhone.*Mobile|\biPod',