PHP detects mobile device mobiledetection instances

Source: Internet
Author: User
This article describes how to use PHP to detect mobiledetection for mobile devices. mobiledetection can detect the operating systems and browsers of most mobile phones and tablets. For more information, see the following, it is normal for a website to have 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:
The 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:
The code is as follows:
$ Detect = new Mobile_Detect;
If ($ detect-> isMobile () | $ detect-> isTablet ()){
Echo" ";
} Else {
Echo" ";
}

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.