PHP and JS Determine whether the client is a PC or a mobile device

Source: Internet
Author: User

JS Judgment

<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) = = "and Roid "; var bisce = Suseragent.match (/windows ce/i) = =" Windows CE "; var biswm = Suseragent.match (/windows mobile/i) = =" Wind OWS mobile "; if (Bisipad | | bisiphoneos | | bismidp | | bIsUc7 | | bisuc | | bisandroid | | bisce | | biswm) {    window.locatio N.href = ' http://url/mobile.html ';} else {    window.location = ' http://url/pc.html ';}    }    Browserredirect ();</script>

(Above JS judgment method, personal test down, on Windows Phone8 will be judged as PC)

PHP judgment

/** * To determine if it is accessed via mobile phone*/function IsMobile () {//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        returnStristr ($_server['Http_via'],"WAP") ?true:false; }    //determine the client flag sent by the phone, 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'); //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 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;}

PHP and JS Determine whether the client is a PC or a mobile device

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.