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 download address: 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 the Code code 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 the Code code as follows:
$detect = new Mobile_detect;
if ($detect->ismobile () | | $detect->istablet ()) {
echo " ";
} else {
echo " ";
}
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.bkjia.com/PHPjc/754339.html www.bkjia.com true http://www.bkjia.com/PHPjc/754339.html techarticle 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 have ...