Php determines that the client is a mobile phone device and then jumps to the mobile station-PHP source code

Source: Internet
Author: User
Now, mobile Internet has exceeded PC, and many websites have established mobile websites, including my small websites. There is a problem here. If the customer directly uses his mobile phone to open my primary domain name, how can I make a judgment and jump to the mobile website? The following two functions can achieve this. Now, mobile Internet has exceeded PC, and many websites have established mobile websites, including my small websites. There is a problem here. If the customer directly uses his mobile phone to open my primary domain name, how can I make a judgment and jump to the mobile website? The following two functions can achieve this.

Script ec (2); script

The following are the functions I am using, but it seems that there is something wrong with the iphone. If you have a better solution, you can leave a message and send it to me. Thank you very much.

The Code is as follows:
// If it is set on the mobile phone, jump to the mobile phone page
If (isMobile ()){
Header ("Location: mobile. php ");
Exit ();
}


/**
* Determine whether a user is a Mobile user and whether the user is a Mobile user. You can determine the iphone, Android, Windows Mobile, and general Mobile phone systems.
*/
Function isMobile (){
// Determine the iphone and ipad. Do not jump
$ UserAgent = $ _ SERVER ['HTTP _ USER_AGENT '];
If (strpos ($ userAgent, "iPhone") |
Strpos ($ userAgent, "iPad") |
Strpos ($ userAgent, "iPod") |
Strpos ($ userAgent, "iOS ")){
Return false;
}

// If HTTP_X_WAP_PROFILE exists, 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. Some service providers will block this information.
If (isset ($ _ SERVER ['HTTP _ VIA ']) {
// Flase cannot be found; otherwise, true
Return stristr ($ _ SERVER ['HTTP _ vean'], "wap ")? True: false;
}

// Determine the client flag sent by the mobile phone. The compatibility needs to be improved.
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'
);

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

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.