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.