PHP环境下判断客户端是否为手机浏览器
2011-12-29PHP环境下怎么判断客户端是不是手机呢?我上网Google下,发现那真是众说纷纭啊,各有各的方法,但都不尽如人意。于是,我就把这些代码拼凑了一下,效果还不错。在这里和大家分享一下。
//判断是否为移动浏览器
function check_wap() {
if (isset($_SERVER['HTTP_VIA'])) return true;
if (isset($_SERVER['HTTP_X_NOKIA_CONNECTION_MODE'])) return true;
if (isset($_SERVER['HTTP_X_UP_CALLING_LINE_ID'])) return true;
if (strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0) {
// Check whether the browser/gateway says it accepts WML.
$br = "WML";
} else {
$browser = isset($_SERVER['HTTP_USER_AGENT']) ? trim($_SERVER['HTTP_USER_AGENT']) : '';
if(empty($browser)) return true;
$browser=substr($browser,0,4);
if ($browser=="Noki" || // Nokia phones and emulators
$browser=="Eric" || // Ericsson WAP phones and emulators
$browser=="WapI" || // Ericsson WapIDE 2.0
$browser=="MC21" || // Ericsson MC218
$browser=="AUR" || // Ericsson R320
$browser=="R380" || // Ericsson R380
$browser=="UP.B" || // UP.Browser
$browser=="WinW" || // WinWAP browser
$browser=="UPG1" || // UP.SDK 4.0
$browser=="upsi" || // another kind of UP.Browser ??
$browser=="QWAP" || // unknown QWAPPER browser
$browser=="Jigs" || // unknown JigSaw browser
$browser=="Java" || // unknown Java based browser
$browser=="Alca" || // unknown Alcatel-BE3 browser (UP based?)
$browser=="MITS" || // unknown Mitsubishi browser
$browser=="MOT-" || // unknown browser (UP based?)
$browser=="My S" ||// unknown Ericsson devkit browser ?
$browser=="WAPJ" || // Virtual WAPJAG www.wapjag.de
$browser=="fetc" || // fetchpage.cgi Perl script from www.wapcab.de
$browser=="ALAV" || // yet another unknown UP based browser ?
$browser=="Wapa" || // another unknown browser (Web based "Wapalyzer"?)
$browser=="Oper") // Opera
{
$br = "WML";
} else {
$br = "HTML";
}
}
if($br == "WML") {
return TRUE;
} else {
return FALSE;
}
}
if(check_wap()){
//是移动浏览器
header('HTTP/1.1 301 Moved Permanently');//发出301头部
header('Location: http://wap.ehan.cn/');//跳转到移动版本网站
exit();
}
else{
header('HTTP/1.1 301 Moved Permanently');//发出301头部
header('Location: https://mawenjian.net/');//跳转到PC版本网站
exit();
}
各位网友可以使用各种设备尝试打开http://url.ehan.cn/,我相信效果肯定让你满意。
发表回复