Home >Backend Development >PHP Tutorial >PHP determines whether the visitor is a mobile client instance_PHP tutorial
Mobile Internet has become very popular recently. We need to build a PC site and a WAP site. To realize that if the user accesses the WAP site from a computer, he will automatically enter the PC site, and vice versa. I have compiled some codes below for you to take a look at.
Method 1: Determine HTTP_USER_AGENT
The code is as follows | Copy code | ||||||||
if(strpos($agent,"netfront") || strpos($agent,"iphone") || strpos($agent,"midp-2.0") || strpos($agent,"opera mini") || strpos ($agent,"ucweb") || strpos($agent,"android") || strpos($agent,"windows ce") || strpos($agent,"symbianos")) {
|
The code is as follows | Copy code |
if (isset($_SERVER['HTTP_ACCEPT']) && (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')) )) {//Mobile access <🎜> Header("HTTP/1.1 301 Moved Permanently"); <🎜> header("Location:####"); die; <🎜> } |
The above two methods have limitations,
The following two methods are combined to determine
The code is as follows | Copy code |
function isMobile() { |
The above method also has some minor problems. Here I tell you based on my own experience that we can use the screen width to implement and add the machine type, because sometimes the HTTP_USER_AGENT information is not defined above, but the above implementation It is almost compatible with mainstream mobile phones.
We can also use js