Detects mobile device php code and mobile phone access

Source: Internet
Author: User
To determine whether a website is accessed by a mobile user or a PC user, we only need to obtain the user's HTTP_USER_AGENT. I first introduced a general Mobile_Detect, the following two examples are self-written and hope to help you. php... to determine whether a website is accessed by a mobile user or a PC user, we only need to obtain the user's HTTP_USER_AGENT. I first introduced a general Mobile_Detect, the following two examples are self-written and hope to help you.

The php code example is as follows:

 IsMobile () // Check for any tablet. if ($ detect-> isTablet () // Check for any mobile device, excluding tablets. if ($ detect-> isMobile ()&&! $ Detect-> isTablet () if ($ detect-> isMobile ()&&! $ Detect-> isTablet () // Alternative to $ detect-> isAndroidOS () $ detect-> is ('androiddos '); // Batch usageforeach ($ userAgents as $ userAgent) {$ detect-> setUserAgent ($ userAgent); $ isMobile = $ detect-> isMobile ();} // Version check. $ detect-> version ('iPad '); // 4.3 (float)?>

Php judges mobile phone access, the code is as follows:

 Window. location. href = "'. $ string. '"; script';} $ string = str_replace (array (" r "," n "), array ('', ''), $ string ); if (preg_match ('/^ s * location:/is', $ string) {@ header ($ string. "n", $ replace); exit ();} if (emptyempty ($ http_response_code) | PHP_VERSION <'4. 3 ') {@ header ($ string, $ replace);} else {@ header ($ string, $ replace, $ http_response_code) ;}}?>

The js code is as follows:

var isIPhone = /iPhone/i.test(navigator.userAgent),isIPad = /iPad/i.test(navigator.userAgent),isAndroid = /android/i.test(navigator.userAgent);var isIOS = isIPhone || isIPad;alert(    "iPhone? " + isIPhone + "tr" +    "iPad? " + isIPad + "tr" +    "Android? " + isAndroid + "tr" +    "iOS? " + isIOS);


Address:

Reprinted at will, but please attach the article address :-)

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.