At present, a website has more than one version is very normal, such as the PC version, 3G version, mobile version and so on. Depending on the browsing device we need to be directed to different versions. Not only that, we sometimes need to load different CSS depending on the client, so we need to be able to detect the browsing device so that we need to use the "mobile detection" class library.
"Mobile Detection" is a PHP class library for lightweight mobile device detection that uses the User-agent string in conjunction with a specific HTTP header to detect the Mobile client environment. Note that mobile detection is just a server-side (PHP) detection tool and does not replace responsive web design or any other form of client functionality detection.
Mobile Detection class Library: Https://github.com/serbanghita/Mobile-Detect
Example 1: Redirect to another version based on device
When we use mobile device to browse a website, we need to direct to the mobile version of the website, first will have the detection function of the file Mobile_ Detect.php is included in the page or on the home page, and now we are redirected to m.uncletoo.com when browsing the www.uncletoo.com site:
Copy CodeThe code is as follows:
/* Change path information based on file location */
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 site, and there are other forms of redirection:
All tablet devices
if ($detect->istablet ()) {
}
is mobile but not tablet device
if ($detect->ismobile () &&! $detect->istablet ()) {
}
iOS system
if ($detect->isios ()) {
}
Android system
if ($detect->isandroidos ()) {
}
WindowsPhone system
if ($detect->iswindowsphoneos ()) {
}
Example 2: Loading different resources according to different devices
As mentioned above, we can also load different CSS files according to different browsing devices. Such as:
Copy CodeThe code is as follows:
$detect = new Mobile_detect;
if ($detect->ismobile () | | $detect->istablet ()) {
echo "<link rel= ' stylesheet ' href= ' mobile.css type= ' text/css '/>";
} else {
echo "<link rel= ' stylesheet ' href= ' style.css type= ' text/css '/>";
}
Note that mobile detection is a device detection platform, and as technology advances there will be different devices, so you need to update the class library at any time to ensure the accuracy of the detection.
Http://www.jb51.net/article/48991.htm
PHP Detection Mobile Detection Use instance