Php determines whether a visitor is a mobile client instance

Source: Internet
Author: User

Recently, mobile internet has become popular. We need to build a PC station and a WAP station. If a user accesses a WAP station on a computer, the user will automatically enter the PC station. The opposite is true, now I have sorted out some code for your reference.

Method 1: Determine HTTP_USER_AGENT

The Code is as follows: Copy code

$ Agent = strtolower ($ _ SERVER ['HTTP _ USER_AGENT ']);
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 ")){
Header ("HTTP/1.1 301 Moved Permanently ");
Header ("Location: ####"); die;
}


Method 2: Determine HTTP_ACCEPT

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;
}

Both methods have limitations,

The following two methods are combined to determine

The Code is as follows: Copy code

Function isMobile (){
If (isset ($ _ SERVER ['HTTP _ X_WAP_PROFILE ']) {
Return true;
}
If (isset ($ _ SERVER ['HTTP _ VIA ']) {
// Flase cannot be found; otherwise, true
Return stristr ($ _ SERVER ['HTTP _ vean'], "wap ")? True: false;
}
If (isset ($ _ SERVER ['HTTP _ USER_AGENT ']) {
// This array needs to be improved
$ 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'
);
// Search for the keyword of the mobile browser from HTTP_USER_AGENT
If (preg_match ("/(". implode ('|', $ clientkeywords ). ")/I", strtolower ($ _ SERVER ['HTTP _ USER_AGENT ']) {
Return true;
}

}

// Resolution, which may be inaccurate and placed in the final judgment
If (isset ($ _ SERVER ['HTTP _ ACCEPT ']) {
// If only wml is supported and html is not supported, it must be a mobile device.
// If wml and html are supported, but wml is a mobile device 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;
}

The above method also has some minor problems. Here I will tell you how big we can use the screen width to add the machine type, sometimes the HTTP_USER_AGENT information is not defined on us, but the above implementation is almost compatible with mainstream mobile phones.

We can also use js

<Html>

<Body>

<Script type = "text/javascript">
Function browserRedirect (){
Var sUserAgent = navigator. userAgent. toLowerCase ();
Var bIsIpad = sUserAgent. match (/ipad/I) = "ipad ";
Var bIsIphoneOs = sUserAgent. match (/iphone OS/I) = "iphone OS ";
Var bIsMidp = sUserAgent. match (/midp/I) = "midp ";
Var bIsUc7 = sUserAgent. match (/rv: 1.2.3.4/I) = "rv: 1.2.3.4 ";
Var bIsUc = sUserAgent. match (/ucweb/I) = "ucweb ";
Var bIsAndroid = sUserAgent. match (/android/I) = "android ";
Var bIsCE = sUserAgent. match (/windows ce/I) = "windows ce ";
Var bIsWM = sUserAgent. match (/windows mobile/I) = "windows mobile ";

If (bIsIpad | bIsIphoneOs | bIsMidp | bIsUc7 | bIsUc | bIsAndroid | bIsCE | bIsWM ){
Window. location. href = 'HTTP: // url/mobile.html ';
} Else {
Window. location = 'HTTP: // url/pc.html ';
}

}

BrowserRedirect ();
</Script>

</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.