PHP function to judge mobile terminal and PC terminal

/* mobile terminal judgment */
	function isMobile()
	{
	    // If there is HTTP_X_WAP_PROFILE, it must be a mobile device
	    if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
	    {
	        return true;
	    }
	    // If the via information contains wap, it must be a mobile device, and some service providers will block this information
	    if (isset ($_SERVER['HTTP_VIA']))
	    {
	        // false if not found, true otherwise
	        return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
	    }
	    // Brain damage method, judge the client logo sent by the mobile phone, the compatibility needs to be improved
	    if (isset ($_SERVER['HTTP_USER_AGENT']))
	    {
	        $clientkeywords = array ('nokia',
	            'sony',
	            'ericsson',
	            'against',
	            'samsung',
	            'htc',
	            'sgh',
	            'lg',
	            'sharp',
	            'she-',
	            'philips',
	            'panasonic',
	            'alcatel',
	            'lenovo',
	            'iphone',
	            'ipod',
	            'blackberry',
	            'meizu',
	            'android',
	            'netfront',
	            'symbian',
	            'ucweb',
	            'windowsce',
	            'palm',
	            'operamini',
	            'operamobi',
	            'openwave',
	            'nexusone',
	            'cldc',
	            'midp',
	            'wap',
	            'mobile'
	            );
	        // Find keywords of mobile browsers from HTTP_USER_AGENT
	        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
	        {
	            return true;
	        }
	    }
	    // Protocol method, because it may be inaccurate, put it in the final judgment
	    if (isset ($_SERVER['HTTP_ACCEPT']))
	    {
	        // If only wml is supported and html is not supported then it must be a mobile device
	        // mobile if wml and html are supported but wml comes before 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'))))
	        {
	            return true;
	        }
	    }
	    return false;
	}

If the calling function returns true, it is a mobile terminal.


        Sometimes, we will configure different domain name access according to the difference between the mobile terminal and the PC terminal. In this regard, we can judge the domain name currently accessed, and then judge the terminal device, and jump to the corresponding domain name according to the judgment. as follows:

// Determine the current access address
$url = $_SERVER['HTTP_HOST'];
if($url == 'mobile url') { //Mobile access address
    if(!isMobile()) {
	// The terminal is a PC, jump to the PC url
    	echo '<script>location.href="https://PC端url地址"</script>';
    	exit;
    }
} else { //pc access address
    if(isMobile()) {
        // The terminal is a mobile terminal, jump to the mobile terminal url
        echo '<script>location.href="https://mobile terminal access address"</script>';
        exit;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325851758&siteId=291194637