php判断电脑访问、手机访问的例子
所以我就研究怎么用php去判断网页是电脑访问还是手机访问,然后再通过去加载不同的css来达到手机和电脑都可以正常的访问的效果。
网站查了很多资料,终于找到了一个合适、比较好用的代码,也希望能给大家一个参考。
<?php
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;
$mobile_os_list=array('google wireless transcoder','windows ce','windowsce','symbian','android','armv6l','armv5','mobile','centos','mowser','avantgo','opera mobi','j2me/midp','smartphone','go.web','palm','ipaq');
$mobile_token_list=array('profile/midp','configuration/cldc-','160×160','176×220','240×240','240×320','320×240','up.browser','up.link','symbianos','palmos','pocketpc','sonyericsson','nokia','blackberry','vodafone','benq','novarra-vision','iris','netfront','htc_','xda_','samsung-sgh','wapaka','docomo','iphone','ipod');
$found_mobile=checksubstrs($mobile_os_list,$browser) ||
checksubstrs($mobile_token_list,$browser);
if($found_mobile)
$br ="wml";
else $br = "www";
}
if($br == "wml") {
return true;
} else {
return false;
}
}
function checksubstrs($list,$str){
$flag = false;
for($i=0;$i<count($list);$i++){
if(strpos($str,$list[$i]) > 0){
$flag = true;
break;
}
}
return $flag;
}
if(check_wap()){
echo "wap";
}else{
echo "web";
}
?>
小编已经测试过了,是可以识别是电脑访问还是手机访问的。