在开发网站时,访问者如果是使用手机浏览器访问我们的站点,会受到一些限制,为此我们需要单独的开发出站点的移动版本,并且在站点的首页加上判断,若访问者是用手机或者其它移动设备访问,页面自动跳转至wap页面。若是PC访问则打开正常页面。怎样去实现呢?
下面是引用一段discuz! x2的代码 ,判断页面是否是手机移动设备访问,若是,则页面输出'wap',立即自动跳转到 'mobile.html' 这个网页,如果是PC访问,则在页面上输出 'PC' ,停顿一秒钟后就自动跳转到 'pc.html' 这个网页.
<?php
function checkmobile() {
global $_G;
$mobile = array();
static $mobilebrowser_list
=array('iphone', 'android', 'phone', 'mobile',
'wap', 'netfront', 'java', 'opera mobi',
'opera mini','ucweb', 'windows ce',
'symbian', 'series', 'webos', 'sony',
'blackberry', 'dopod', 'nokia', 'samsung',
'palmsource', 'xda', 'pieplus', 'meizu',
'midp', 'cldc', 'motorola', 'foma', 'docomo',
'up.browser','up.link', 'blazer', 'helio',
'hosin', 'huawei', 'novarra', 'coolpad',
'webos', 'techfaith', 'palmsource',
'alcatel', 'amoi', 'ktouch', 'nexian',
'ericsson', 'philips', 'sagem', 'wellcom',
'bunjalloo', 'maui', 'smartphone',
'iemobile', 'spice', 'bird', 'zte-',
'longcos', 'pantech', 'gionee', 'portalmmm',
'jig browser', 'hiptop', 'benq', 'haier',
'^lct', '320x320', '240x320', '176x220'
);
$useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
if(($v = dstrpos($useragent, $mobilebrowser_list, true))) {
$_G['mobile'] = $v;
return true;
}
$brower = array('mozilla', 'chrome', 'safari', 'opera',
'm3gate', 'winwap', 'openwave', 'myop');
if(dstrpos($useragent, $brower))
return false;
$_G['mobile'] = 'unknown';
if($_GET['mobile'] === 'yes') {
return true;
}
else {
return false;
}
}
function dstrpos($string, &$arr, $returnvalue = false) {
if(empty($string))
return false;
foreach((array)$arr as $v) {
if(strpos($string, $v) !== false) {
$return = $returnvalue ? $v : true;
return $return;
}
}
return false;
}
if (checkmobile())
{
echo 'wap' ;
?>
<meta http-equiv="refresh" content="0;url=mobile.html">
<?php
}
else
{
echo 'pc';
?>
<meta http-equiv="refresh" content="1;url=pc.html">
<?php
}
?>
对于上面的代码,在最后我切换到了html代码跳转到不同的页面。
可以使用PC和手机端分别访问地址:gongziya.com/test/testwap,可以看出这两种访问方式访问到了不同的页面。