PHP methods for judging mobile devices such as mobile phones

Source: Internet
Author: User
Now that the mobile device is hot, we may often need to match the page of the mobile device when we do the Web front-end development. Of course we can use the responsive page design to handle the front-end matching problem, but the responsive page just doesn't show some code, but it still loads into the user's mobile device.

In order to better take care of the user's mobile browsing experience, reduce unnecessary loading. We can then use PHP to reduce the user's code load, improve the user experience when browsing the page.

Because of the variety of mobile devices now, and the wide range of browsers, it is only through the UA to judge that the problem is not completely solved.

Here is a PHP ready-made method for PHP to make mobile device judgment

<?phpfunction IsMobile () {///If there is http_x_wap_profile, 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 associations block the information if (Isset ($_server[' Http_via ')) {//Cannot find for flase, otherwise true Retu RN 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 ', ' Andro            Id ', ' 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 that must be a mobile device/ /if WML and HTML are supported but WML before HTML is the mobile device 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;}? >

At present, this method can be used for most mobile device judgment, can also be used to add some of the UA parameters.

Of course, if you use the WP architecture, the inside of this function is integrated.
if (Wp_is_moblile ()) {
Echo ' You are using mobile device browsing ';
}else{
Echo ' You are not currently using mobile devices ';
}

  • 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.