Home >Backend Development >PHP Tutorial >PHP detection mobile device class mobile detection usage example_PHP tutorial

PHP detection mobile device class mobile detection usage example_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:32:46887browse

Currently, it is normal for a website to have multiple versions, such as PC version, 3G version, mobile version, etc. Depending on the browsing device we need to be directed to different versions. Not only that, we sometimes need to load different CSS according to 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 lightweight mobile device detection PHP class library that uses a combination of User-Agent strings in specific HTTP headers to detect mobile client environments. Note that mobile detection is only a server-side (PHP) detection tool and cannot replace responsive web design or any other form of client-side function detection.

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

Example 1: Redirect to other versions based on device

When we use a mobile device to browse a website, we need to be directed to the mobile version of the website. First, include the file Mobile_Detect.php with detection function into the web page or home page. Now let’s browse www.uncletoo.com When the website is redirected to m.uncletoo.com:

Copy the code The code is as follows:

/*Change according to the file location Path information*/
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
if($detect->isMobile()) {
header('Location: http:// m.uncletoo.com/');
exit;
}
This is directed to the mobile website, there are other forms of redirection below:
//All tablet devices
if( $detect->isTablet()) {
}
//It is a mobile but not a 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. For example:

Copy code The code is as follows:

$detect = new Mobile_Detect;
if($detect-> isMobile() || $detect->isTablet()) {
echo "";
} else {
echo "";
}

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/754339.htmlTechArticleCurrently, it is normal for a website to have multiple versions, such as PC version, 3G version, mobile version, etc. . Depending on the browsing device we need to be directed to different versions. Not only that, we have...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn