文章分类

在线反馈

可以将问题或建议反馈给我们。
检测用户是用手机(Mobile)还是电脑(PC)访问网站

1. php  mobile detect

Php代码  收藏代码
  1. <?php  
  2.   
  3. /** 
  4.  * Mobile Detect 
  5.  * 
  6.  * @license    http://www.opensource.org/licenses/mit-license.php The MIT License 
  7.  * @version    SVN: $Id: Mobile_Detect.php 4 2011-05-26 08:04:20Z vic.stanciu@gmail.com $ 
  8.  */  
  9.   
  10. class Mobile_Detect {  
  11.       
  12.     protected $accept;  
  13.     protected $userAgent;  
  14.       
  15.     protected $isMobile     = false;  
  16.     protected $isAndroid    = null;  
  17.     protected $isBlackberry = null;  
  18.     protected $isOpera      = null;  
  19.     protected $isPalm       = null;  
  20.     protected $isWindows    = null;  
  21.     protected $isGeneric    = null;  
  22.   
  23.     protected $devices = array(  
  24.         "android"       => "android",  
  25.         "blackberry"    => "blackberry",  
  26.         "iphone"        => "(iphone|ipod)",  
  27.         "opera"         => "opera mini",  
  28.         "palm"          => "(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)",  
  29.         "windows"       => "windows ce; (iemobile|ppc|smartphone)",  
  30.         "generic"       => "(kindle|mobile|mmp|midp|o2|pda|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap)"  
  31.     );  
  32.   
  33.   
  34.     public function __construct() {  
  35.         $this->userAgent = $_SERVER['HTTP_USER_AGENT'];  
  36.         $this->accept    = $_SERVER['HTTP_ACCEPT'];  
  37.   
  38.         if (isset($_SERVER['HTTP_X_WAP_PROFILE'])|| isset($_SERVER['HTTP_PROFILE'])) $this->isMobile = true;  
  39.         elseif (strpos($this->accept,'text/vnd.wap.wml') > 0 || strpos($this->accept,'application/vnd.wap.xhtml+xml') > 0) $this->isMobile = true;  
  40.         else foreach ($this->devices as $device => $regexp) if ($this->isDevice($device)) $this->isMobile = true;  
  41.     }  
  42.   
  43.   
  44.     /** 
  45.      * Overloads isAndroid() | isBlackberry() | isOpera() | isPalm() | isWindows() | isGeneric() through isDevice() 
  46.      * 
  47.      * @param string $name 
  48.      * @param array $arguments 
  49.      * @return bool 
  50.      */  
  51.     public function __call($name, $arguments) {  
  52.         $device = substr($name, 2);  
  53.         if ($name == "is" . ucfirst($device)) return $this->isDevice($device);  
  54.         else  trigger_error("Method $name not defined", E_USER_ERROR);  
  55.     }  
  56.   
  57.   
  58.     /** 
  59.      * Returns true if any type of mobile device detected, including special ones 
  60.      * @return bool 
  61.      */  
  62.     public function isMobile() {  
  63.         return $this->isMobile;  
  64.     }  
  65.   
  66.   
  67.     protected function isDevice($device) {  
  68.         $var    = "is" . ucfirst($device);  
  69.         $return = $this->$var === null ? (bool) preg_match("/" . $this->devices[$device] . "/i", $this->userAgent) : $this->$var;  
  70.   
  71.         if ($device != 'generic' && $return == true) $this->isGeneric = false;  
  72.   
  73.         return $return;  
  74.     }  
  75. }  

Description

Mobile_Detect is a simple PHP class for easy detection of the most popular mobile devices platforms: Android, Blackberry, Opera Mini, Palm, Windows Mobile, as well as generic ones.

Usage

Include and instantiate the class:

 

include("Mobile_Detect.php");
$detect = new Mobile_Detect();

 

Check for a specific platform:

 

if ($detect->isAndroid()) {
    // code to run for the Google Android platform
}

 

Available methods are isAndroid(), isBlackberry(), isOpera(), isPalm(), isWindows(), isGeneric(). Alternatively, if you are only interested in checking to see if the user is using a mobile device, without caring for specific platform:

 

if ($detect->isMobile()) {
    // any mobile platform
}

 

来源:http://code.google.com/p/php-mobile-detect/

在提供一个各种语言检测的网址:http://detectmobilebrowsers.com/

在线客服
send (97526501)
send (97526502)
send (97526503)
Tel:021-35110083