Maison  >  Article  >  développement back-end  >  Fonction PHP pour déterminer le terminal mobile et le terminal PC

Fonction PHP pour déterminer le terminal mobile et le terminal PC

不言
不言original
2018-04-26 10:30:472860parcourir

Le contenu de cet article concerne la fonction PHP pour juger le terminal mobile et le terminal PC. Il a une certaine valeur de référence. Maintenant, je le partage avec vous. Les amis dans le besoin peuvent s'y référer

/*移动端判断*/
	function isMobile()
	{ 
	    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
	    if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
	    {
	        return true;
	    } 
	    // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
	    if (isset ($_SERVER['HTTP_VIA']))
	    { 
	        // 找不到为flase,否则为true
	        return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
	    } 
	    // 脑残法,判断手机发送的客户端标志,兼容性有待提高
	    if (isset ($_SERVER['HTTP_USER_AGENT']))
	    {
	        $clientkeywords = 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'
	            ); 
	        // 从HTTP_USER_AGENT中查找手机浏览器的关键字
	        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
	        {
	            return true;
	        } 
	    } 
	    // 协议法,因为有可能不准确,放到最后判断
	    if (isset ($_SERVER['HTTP_ACCEPT']))
	    { 
	        // 如果只支持wml并且不支持html那一定是移动设备
	        // 如果支持wml和html但是wml在html之前则是移动设备
	        if ((strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;vnd.wap.wml&#39;) !== false) && (strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;text/html&#39;) === false || (strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;vnd.wap.wml&#39;) < strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;text/html&#39;))))
	        {
	            return true;
	        } 
	    } 
	    return false;
	}


Si le résultat de l'appel de la fonction est vrai, il s'agit du terminal mobile.


Parfois, nous configurerons différents accès au nom de domaine en fonction des différences entre le terminal mobile et le terminal PC. À cet égard, nous pouvons déterminer le nom de domaine actuellement consulté, puis déterminer le terminal et accéder au nom de domaine correspondant en fonction du jugement. Comme suit :

// 判断当前访问地址
$url = $_SERVER[&#39;HTTP_HOST&#39;];
if($url == &#39;移动端url&#39;) {	//移动端访问地址
    if(!isMobile()) {
	// 终端是PC,跳转到PC端url
    	echo &#39;<script>location.href="https://PC端url地址"</script>&#39;;
    	exit;
    }
} else {	                //pc访问地址
    if(isMobile()) {
        // 终端是移动端,跳转到移动端url
        echo &#39;<script>location.href="https://移动端访问地址"</script>&#39;;
        exit;
    }
}
Recommandations associées :

La fonction PHP détermine si l'accès se fait depuis un navigateur d'ordinateur ou un navigateur mobile

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn