PHP Detection Mobile Detection Use instance _php instance

Source: Internet
Author: User
Tags php class

At present, a website has several versions is very normal, such as PC version, 3G version, mobile version and so on. Depending on the browsing device we need to be directed to a different version. 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 we need to use the "mobile detection" class library.

Mobile detection is a PHP class library for lightweight mobile device detection that detects a mobile client environment with a user-agent string in a specific HTTP header. Note that mobile detection is just a server-side (PHP) detection tool and is not a substitute for responsive web design or any other form of client-side functionality detection.

Mobile detection class Library download address: Https://github.com/serbanghita/Mobile-Detect

Example 1: Redirect to another version depending on the device

When we use a mobile device to browse a website, we need to be directed to the mobile version of the site, first the file with the detection function Mobile_ Detect.php is included in the Web page or the home page, now we are redirecting to m.uncletoo.com when browsing the www.uncletoo.com Web site:

Copy Code code as follows:

/* Change path information according to 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 a mobile web site, and there are other forms of redirection below:
All tablet devices
if ($detect->istablet ()) {
}
is a mobile but not a flatbed 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 based on different devices

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

Copy Code code 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 progresses there will be different devices, so you need to update the class library at any time to ensure the accuracy of the inspection.

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.