JavaScript and PHP differentiate between mobile devices and PC access sites

Source: Internet
Author: User


One, JS implementation

Window.onload=function () {
var mobileagent = new Array ("iphone", "ipod", "ipad", "Android", "mobile", "BlackBerry", "webOS",

"Incognito", "webmate", "Bada", "Nokia", "LG", "UCWeb", "Skyfire");
var browser = Navigator.userAgent.toLowerCase ();
var ismobile = false;
for (var i=0; i<mobileagent.length; i++) {

if (Browser.indexof (Mobileagent[i])!=-1) {ismobile = true;
Alert (Mobileagent[i]);
Location.href = ' http://www.111cn.net ';
Break

}

}
}

<title> Testing </title>
<meta Charset=utf8/>
<body>
<?php Echo ' Hello Wap '?>
<script src=./test.js></script>
</body>

Test: When http://test92.sinaapp.com/is opened with PC, show Hello WAP, when accessing with mobile device, will jump www.111cn.net. (You can test the mobile-side access site directly in Chrome)

Second, PHP implementation

PHP detects mobile devices with the help of a mobile detection class library. Mobile detection class Library download address: Https://github.com/dwqs/Mobile-Detect

<title> Testing </title>
<meta Charset=utf8/>
<body>
<p>mobile_detect.php Identification Cell Phone </p>
<?php
Require_once "mobile_detect.php";
$detect = new Mobile_detect;
if ($detect->ismobile ()) {
echo "<script>location.href= ' http://www.ido321.com/' </script>";
}
?>

</body>

Testing: http://www.111cn.net/can also individually identify mobile systems and devices

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 ()) {
}

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.