Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use PHP to detect Mobile device Class mobile detection

Shulou Source: shulou.com Published: 2022-06-03 07:53:24 10月01日 Update

This article mainly explains "how to use PHP to detect mobile device mobile detection". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use PHP to detect mobile device mobile detection".

At present, it is normal for a website to have multiple versions, such as PC version, 3G version, mobile version and so on. We need to direct to different versions according to different browsing devices. Not only that, we sometimes need to load different CSS according to different clients, so we need to be able to detect browsing devices, SO, we need to use the "mobile detection" class library.

"mobile detection" is a lightweight PHP class library for mobile device detection, which detects the mobile client environment by combining User-Agent strings in specific HTTP headers. Note that mobile detection is only a server-side (PHP) detection tool and is not a substitute for responsive Web design or any other form of client-side functional detection.

Download address of mobile detection class library: https://github.com/serbanghita/Mobile-Detect

Example 1: redirect to another version based on the device

When we use a mobile device to browse a website, we need to direct to the mobile version of the website. First, include the file Mobile_Detect.php with detection function in the web page or home page. Now let's redirect to m.uncletoo.com when browsing the www.uncletoo.com website:

The copy code is as follows:

/ * change the path information according to the file location * /

Require_once 'Mobile_Detect.php'

$detect = new Mobile_Detect

If ($detect- > isMobile ()) {

Header ('Location: http://m.uncletoo.com/');

Exit

}

This is a redirect to a mobile website, and there are other forms of redirection below:

/ / all tablet devices

If ($detect- > isTablet ()) {

}

/ / is a mobile but non-tablet device

If ($detect- > isMobile () & &! $detect- > isTablet ()) {

}

/ / IOS system

If ($detect- > isiOS ()) {

}

/ / Android system

If ($detect- > isAndroidOS ()) {

}

/ / WindowsPhone system

If ($detect- > isWindowsPhoneOS ()) {

}

Example 2: load different resources according to different devices

As mentioned above, we can also load different CSS files according to different browsing devices. Such as:

The copy code is as follows:

$detect = new Mobile_Detect

If ($detect- > isMobile () | | $detect- > isTablet ()) {

Echo ""

}

Note that mobile detection is a mobile device detection platform, with the development of technology, different devices will appear, so you need to update the class library at any time in order to ensure the accuracy of detection.

Thank you for reading, the above is the content of "how to use PHP to detect mobile device mobile detection". After the study of this article, I believe you have a deeper understanding of how to use PHP to detect mobile device mobile detection, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Device detection mobile different website browsing code customer client file version system learning content function instance tablet form not only that location Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker OPPO Reno NVidia Shulou Tech Info Apple