PHP function to determine the mobile and PC side

Source: Internet
Author: User
This article introduces the content of PHP function to determine the mobile side and PC side, has a certain reference value, now share to everyone, the need for friends can refer to

/* Mobile judgment */function IsMobile () {//If there is http_x_wap_profile must be the 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 associations block the information if (Isset ($_server[' Http_via ')) {//Cannot find for flase, otherwise true re Turn stristr ($_server[' Http_via '), "WAP")?    True:false; }//Brain residue method, determine the mobile phone sent by the client flag, 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            OID ', ' NetFront ', ' Symbian ', ' UCWeb ', ' windowsce ', ' palm ',   ' Operamini ', ' operamobi ', ' Openwave ',         ' Nexusone ', ' cldc ', ' MIDP ', ' wap ', ' mobile '; Look for the keyword if (Preg_match ("/(") of the phone's browser from Http_user_agent. Implode (' | ', $clientkeywords).        ")/I", Strtolower ($_server[' http_user_agent '))) {return true;        }}//protocol method, because it may not be accurate, put to the Last 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 before HTML the mobile device if ((Strpos ($_server[' http_accept '), ' VND.WAP.WML ')!== false) && (STRP OS ($_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 a result of true, it is the mobile side.


Sometimes, we will configure different domain access according to the difference between mobile and PC side. For this, we can judge the current access to the domain name, and then judge the end device, according to the judgment jump to the corresponding domain name access. As follows:

To determine the current access address, $_server[' Http_host '];if ($url = = ' Mobile URL ') {//Mobile Access address    if (!ismobile ()) {//terminal is PC, jump to PC-side URL    Echo ' <script>location.href= ' https://PC-end URL "</script>";    Exit;    }} else {                //PC access address    if (IsMobile ()) {        //terminal is mobile, jump to mobile URL        echo ' <script>location.href= ' https:// Mobile-access Address "</script>";        Exit;    }}

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.