一、js实现
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;
}
}
}
<html>
<head>
<title>测试</title>
<meta charset=utf8 />
</head>
<body>
<?php echo 'Hello Wap' ?>
<script src=./test.js></script>
</body>
</html>
测试:http://test92.sinaapp.com/ 用PC打开时,显示Hello WAP,用移动设备访问时,将跳转www.111cn.net.(可以直接在chrome中测试移动端访问站点)
二、php实现
PHP检测移动设备需要借助mobile detection类库。mobile detection 类库下载地址:https://github.com/dwqs/Mobile-Detect
<html>
<head>
<title>测试</title>
<meta charset=utf8 />
</head>
<body>
<p>Mobile_Detect.php识别手机</p>
<?php
require_once "Mobile_Detect.php";
$detect = new Mobile_Detect;
if($detect->isMobile()) {
echo "<script>location.href='http://www.ido321.com/'</script>";
}
?>
</body>
</html>
测试:http://www.111cn.net/ 也可以单独对移动系统和设备进行识别
//所有平板设备
if( $detect->isTablet()) {
}
//是移动但非平板设备
if( $detect->isMobile() && !$detect->isTablet()) {
}
//IOS系统
if( $detect->isiOS()) {
}
//Android系统
if( $detect->isAndroidOS()) {
}
//WindowsPhone系统
if( $detect->isWindowsPhoneOS()) {
}