php中怎样实现判断是手机访问还是电脑浏览器访问
发布网友
发布时间:2022-04-06 00:49
我来回答
共3个回答
热心网友
时间:2022-04-06 02:19
严格的讲 strpos($_SERVER['HTTP_ACCEPT'],'wap')!==false 判断是不够的,网上找了段比较全的测试无误,贴出来mark下:
/**
* 判断是手机访问pc访问
*/
protected 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 = $this->checkSubstrs($mobile_os_list, $browser) || $this->checkSubstrs($mobile_token_list, $browser);
if ($found_mobile) {
$br = "WML";
} else {
$br = "WWW";
}
}
if ($br == "WML") {
return true;
} else {
return false;
}
}
/**
* 判断手机访问, pc访问
*/
protected 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 ($this->check_wap()) {
echo 'wap';
}else{
echo 'pc';
}
热心网友
时间:2022-04-06 03:37
if(strpos($_SERVER['HTTP_ACCEPT'],'wap')){
手机页面
}else{
电脑页面
}
热心网友
时间:2022-04-06 05:11
<?php
if(strpos($_SERVER['HTTP_ACCEPT'],'wap')!==false)
{//手机
}else{
//电脑
}