php或js判斷網站訪問者來自手機或者pc機
針對不一樣平臺選擇不一樣的網站版本,最終總結以下:
1、JS版代碼:
<!--切換手機版網站--->
<script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script>
<script type="text/javascript">uaredirect("http://www.zhuqiaodou.com/");</script>
//Js方法二
<script type="text/javascript">
<!-- //平臺、設備和操做系統
var system ={
win : false,
mac : false,
xll : false
};
//檢測平臺
var p = navigator.platform;
system.win = p.indexOf("Win") == 0;
system.mac = p.indexOf("Mac") == 0;
system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);
//跳轉語句,若是是手機訪問就自動跳轉到wap.baidu.com頁面
if(system.win||system.mac||system.xll){
window.location.href="http://www.php186.com";
}else{
window.location.href="http://wap.php186.com";
}
-->
</script>
2、PHP代碼版:
<?php
//判斷手機仍是電腦訪問網站方法一:
function isMobile() {
if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
{
echo "移動設備";
}else{
echo "PC機";
}
}
//判斷手機仍是電腦訪問網站方法二:
if (isset ($_SERVER['HTTP_VIA']))
{
//找不到爲flase,不然爲true
if(stristr($_SERVER['HTTP_VIA'], "wap"))
{
echo "移動設備";
}else{
echo "PC機";
}
}
//判斷手機仍是電腦訪問網站方法三:
if (isset ($_SERVER['HTTP_USER_AGENT']))
{
$clientkeywords = array (
'nokia',
'sony',
'ericsson',
'mot',
'samsung',
'htc',
'sgh',
'lg',
'sharp',
'sie-',
'philips',
'panasonic',
'alcatel',
'lenovo',
'iphone',
'ipod',
'blackberry',
'meizu',
'android',
'netfront',
'symbian',
'ucweb',
'windowsce',
'palm',
'operamini',
'operamobi',
'openwave',
'nexusone',
'cldc',
'midp',
'wap',
'mobile'
);
// 從HTTP_USER_AGENT中查找手機瀏覽器的關鍵字
if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
{
echo "移動設備";
}else{
echo "PC機";
}
}
?>
//方法四:綜合以上
<?php
$mobile_browser = '0';
if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
$mobile_browser++;
}
if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
$mobile_browser++;
}
$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
$mobile_agents = array(
'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
'wapr','webc','winw','winw','xda','xda-','Googlebot-Mobile');
if(in_array($mobile_ua,$mobile_agents)) {
$mobile_browser++;
}
if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) {
$mobile_browser++;
}
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {
$mobile_browser=0;
}
if($mobile_browser>0) {
header("Location: mobile.php"); //手機版
}else {
header("Location: pc.php"); //電腦版
}
個人最終解決方案選擇方法四,綜合了前面三種:
pc版的主頁:http://www.zhuqiaodou.com
index.php代碼:
<?php
/**判斷是否手機瀏覽器
* @return boolean
*/
function is_moble(){
$mobile_browser = '0';
if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
$mobile_browser++;
}
if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
$mobile_browser++;
}
$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
$mobile_agents = array(
'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
'wapr','webc','winw','winw','xda','xda-','Googlebot-Mobile');
if(in_array($mobile_ua,$mobile_agents)) {
$mobile_browser++;
}
if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) {
$mobile_browser++;
}
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {
$mobile_browser=0;
}
if($mobile_browser>0) {
return true;
//header("Location: mobile.php"); //手機版
}else {
return false;
//header("Location: pc.php"); //電腦版
}
}
if(is_moble()){
header("Location:http://www.zhuqiaodou.com"); //手機版
exit();
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>豬巧豆</title>
</head>
<body>
pc版主頁。。。。
</body>
</html>
javascript
http://www.zhuqiaodou.com/forum.php?mod=viewthread&tid=76&extra=page%3D1php