PHP detects mobile detection instances for mobile devices

Source: Internet
Author: User

Currently, it is normal that a website has multiple versions, such as PC, 3G, and mobile. Based on Different browsing devices, we need to direct them to different versions. In addition, we sometimes need to load different CSS based on different clients, SO we need to be able to detect the browsing device, SO, we need to use the "mobile detection" class library.

"Mobile detection" is a PHP class library for lightweight mobile device detection. It uses the User-Agent string in a specific HTTP header to detect the mobile client environment. Note that mobile detection is only a server-side (PHP) detection tool and cannot be used in concert with responsive Web design or any other form of client function detection.

Mobile detection Library: https://github.com/serbanghita/Mobile-Detect

Instance 1: redirection to another version based on the device

When we use a mobile device to browse a website, we need to direct it to the mobile version of the website. First, we need to include the file Mobile_Detect.php with the detection function into the webpage or home page, now we can redirect to m.uncletoo.com when Browsing www.uncletoo.com:
Copy codeThe Code is as follows:
/* Change the path information based on the file location */
Require_once 'mobile _ Detect. php ';
$ Detect = new Mobile_Detect;
If ($ detect-> isMobile ()){
Header ('location: http://m.uncletoo.com /');
Exit;
}
This is targeted to a mobile website, and there are other types of redirection below:
// All flat Devices
If ($ detect-> isTablet ()){
}
// It is a mobile device but not a flat Device
If ($ detect-> isMobile ()&&! $ Detect-> isTablet ()){
}
// IOS system
If ($ detect-> isiOS ()){
}
// Android system
If ($ detect-> isAndroidOS ()){
}
// WindowsPhone System
If ($ detect-> isWindowsPhoneOS ()){
}

Instance 2: Load different resources according to different devices

As mentioned above, we can also load different CSS files based on different browsing devices. For example:
Copy codeThe Code is as follows:
$ Detect = new Mobile_Detect;
If ($ detect-> isMobile () | $ detect-> isTablet ()){
Echo "<link rel = 'stylesheet 'href}'lele.css type = 'text/css '/> ";
} Else {
Echo "<link rel = 'stylesheet 'href}'style.css type = 'text/css '/> ";
}

Note: mobile detection is a mobile device detection platform. as technology advances, different devices may appear. Therefore, you must update the class library at any time to ensure the accuracy of the detection.

 

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.