09 1 月 2013

怎样判断手机还是pc浏览器

php

代码

1.

<?php
$mobile_browser = 1;

if (strpos(strtolower($_SERVER[‘HTTP_USER_AGENT’]),’windows nt’)>0
|| strpos(strtolower($_SERVER[‘HTTP_USER_AGENT’]),’windows xp’)>0
|| strpos(strtolower($_SERVER[‘HTTP_USER_AGENT’]),’winnt’)>0
|| strpos(strtolower($_SERVER[‘HTTP_USER_AGENT’]),’windows 2000′)>0
|| strpos(strtolower($_SERVER[‘HTTP_USER_AGENT’]),’windows 98′)>0
) {
$mobile_browser=0;
}
if($mobile_browser>0) {
die(require_once( dirname(__FILE__) . ‘http://y.blogfeng.tk’)); // 跳转到手机版的主页
}else {
die(require_once( dirname(__FILE__) . ‘http://feng.cz.cc’)); // 跳转到电脑版的主页
}
?>

 

2.
<?php
function is_wap()
{
$ua=strtolower($_SERVER[‘HTTP_USER_AGENT’]);
$uachar = “/(untrusted|nokia|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|wap|mobile)/i”;
if(($ua == ” || preg_match($uachar, $ua))&& !strpos(strtolower($_SERVER[‘REQUEST_URI’]),’wap’))
{
return true;
}
else
{
return false;
}
}

if(is_wap())
{
header(‘Location: http://y.blogfeng.tk’);
exit;
}
else
{
header(‘Location: http://feng.cz.cc’);
exit;
}
?>