PHP Detection Mobile Detection Use instance

Source: Internet
Author: User
Tags php class

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.