PHP or JS Judge site visitors from the phone or PC-side source code

Source: Internet
Author: User

Many times can also be judged by logic programs, such as PHP, JS is commonly used to identify the two common methods of accessing device types.

The principle is to identify the access client http_user_agent, and then perform keyword matching to determine the device type, for the forgery of the HTTP header can not be distinguished.

Differentiate device type then you have to be able to display easy-to-read Web pages for different settings, specifically to study responsive layout techniques.

This article mainly describes how to use PHP or JS to identify the terminal type.

<?PHPclassIsMobile { Public Static functionIsMobile () {$useragent=isset($_server[' Http_user_agent ']) ?$_server[' Http_user_agent ']: '; $useragent _commentsblock=Preg_match(' |\ (. *?\) | ',$useragent,$matches) >0?$matches[0]: '; $mobile _os_list=Array(' Google Wireless transcoder ', ' Windows CE ', ' windowsce ', ' Symbian ', ' Android ', ' armv6l ', ' armv5 ', ' Mobile ', ' CentOS ', ' Mowser ', ' AvantGo ', ' Opera Mobi ', ' J2ME/MIDP ', ' Smartphone ', ' go.web ', ' Palm ', ' IPAQ '); $mobile _token_list=Array(' PROFILE/MIDP ', ' configuration/cldc-', ' 160x160 ', ' 176x220 ', ' 240x240 ', ' 240x320 ', ' the ' + ', ' up '. Browser ', ' up. Link ', ' SymbianOS ', ' PalmOS ', ' PocketPC ', ' SonyEricsson ', ' Nokia ', ' BlackBerry ', ' Vodafone ', ' BenQ ', ' novarra-vision ', ' Iris ', ' NetFront ', ' htc_ ', ' Xda_ ', ' samsung-sgh ', ' Wapaka ', ' DoCoMo ', ' iPhone ', ' IPod '); $found _mobile= Self::checksubstrs ($mobile _os_list,$useragent _commentsblock) || Self:: Checksubstrs ($mobile _token_list,$useragent); if($found _mobile){            return true; }Else{            return false; }    }     Public Static functionChecksubstrs ($substrs,$text)    {        foreach($substrs  as $substr)            if(false!==Strpos($text,$substr)){            return true; }        return false; }}/*Test Codeif (Ismobile::ismobile ()) echo ' Mobile login www.nginx.cn ', else echo ' computer login www.nginx.cn ';*/?>

functionIsphone () {//If you have http_x_wap_profile, it must be a mobile device .    if(isset($_server[' Http_x_wap_profile '])) {        return true; }    //if the VIA message contains a WAP, it must be a mobile device, and some service providers block that information    if(isset($_server[' Http_via '])) {        //not found for flase, otherwise true        if(Stristr($_server[' Http_via '], "WAP"))        {            return true; }    }    //brain Residue method, determine the mobile phone to send the client logo, compatibility needs to be improved    if(isset($_server[' Http_user_agent '])) {        $clientkeywords=Array (            ' Nokia ', ' Sony ', ' Ericsson ', ' mot ', ' Samsung ', ' HTC ', ' s GH ', ' lg ', ' sharp ', ' sie-', ' Philips ', ' Panasonic ', ' ALC '            Atel ', ' Lenovo ', ' iphone ', ' ipod ', ' blackberry ', ' Meizu ',            ' Android ', ' NetFront ', ' Symbian ', ' UCWeb ', ' windowsce ', ' palm ',            ' Operamini ', ' operamobi ', ' Openwave ', ' nexusone ', ' CLDC ', ' MIDP ', ' wap ', ' mobile ', ' phone ',        ); //find keywords for your phone's browser from Http_user_agent        if(Preg_match("/(" .implode(' | ',$clientkeywords) . ")/I",Strtolower($_server[' Http_user_agent ']))) {            return true; }    }    //protocol law, because there may be inaccuracies, put to the final Judgment    if(isset($_server[' Http_accept '])) {        //If only WML is supported and HTML is not supported that must be mobile device//If WML and HTML are supported but WML is 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;} JS distinguish phone or PC access source method one<!--Switch Mobile website---><script src= "http://siteapp.baidu.com/static/webappservice/uaredirect.js" type= "text/ JavaScript "></script><script type=" Text/javascript ">uaredirect (" http://www.nginx.cn/"); </ Script>Method Two<script type= "Text/javascript" > <!--//platforms, devices, and operating systems    var system={win:false,mac:false,XLL:false    }; //Testing Platform    varp = Navigator.platform; system. Win = P.indexof ("win") = = 0; system. Mac = P.indexof ("mac") = = 0; system. x11 = (P = = "X11") | | (P.indexof ("Linux") = = 0); //Jump Statement, if it is mobile Access will automatically jump to the wap.baidu.com page    if(system. win| |system. mac| |system.xll) {window. location.href= "http://www.kdw.cc"; }Else{window. location.href= "http://m.kdw.cc"; }    --></script>

PHP or JS Judge site visitors from the phone or PC-side source code

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.