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.