其他分享
首页 > 其他分享> > 判断是不是手机端

判断是不是手机端

作者:互联网

public static $bIsMobileEquipment		= null;
	public static $sMobileEquipment			= '';
	public static $aClientUserAgent			= array(
		'nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu', 'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile'
	);
public static function _isMobile(){
		//如果是推发,强制pc端
		if(defined('IS_STATION_CLUSTER')){
			return false;
		}
		// 如果是移动站域名,无论是否是移动端,都不判断 2019-01-02 去除域名强制
		if ( self::_isMobileDomain() ){
			self::_setEqui();
			return true;
		}
		// 如果有HTTP_X_WAP_PROFILE则一定是移动设备	
		if ( isset($_SERVER['HTTP_X_WAP_PROFILE']) ){
			self::_setEqui();
			return true;
		}
		//如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息	
		if ( isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap") ){
			self::_setEqui();
			return true;
		}
		//判断手机发送的客户端标志,兼容性有待提高	
		if ( isset($_SERVER['HTTP_USER_AGENT']) ) {	
			if ( strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'ipad') !== false ) return false;
			if ( self::_setEqui() ) return true;
		}	
		//协议法,因为有可能不准确,放到最后判断	
		if ( isset($_SERVER['HTTP_ACCEPT']) ) {	
			// 如果只支持wml并且不支持html那一定是移动设备	
			// 如果支持wml和html但是wml在html之前则是移动设备	
			if ( (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) 
			&& (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || 
			(strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < 
			strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))){
				self::_setEqui();
				return true;
			}
		}	
		return false;		
	}
	public static function _isMobileDomain(){
		if ( !isset($_SERVER['HTTP_HOST']) ) return false;
		if ( sameDomain($_SERVER['HTTP_HOST'],MOBILE_DOMAIN) ) return true;
		if ( sameDomain($_SERVER['HTTP_HOST'],MIP_DOMAIN) ) return true;
		return false;
	}
	
	public static function _setEqui(){
		$aMatch = array();
		if (preg_match("/(".implode('|', self::$aClientUserAgent).")/i", strtolower($_SERVER['HTTP_USER_AGENT']), $aMatch)>0 ){
			self::$sMobileEquipment = $aMatch[1]; return true;
		}else{
			self::$sMobileEquipment = 'unknown';
			return false;	
		}
	}

标签:HTTP,判断,return,self,是不是,SERVER,手机,false,true
来源: https://blog.csdn.net/qq_39999924/article/details/120784786