php判断终端是手机还是电脑访问网站的思路及代码
程序员文章站
2022-11-26 16:21:10
代码一:复制代码 代码如下:
代码一:
<?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";
}
?>
代码二:
<?php
header("content-type:text/html;charset=utf-8");
function is_mobile(){
$user_agent = $_server['http_user_agent'];
$mobile_agents = array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte");
$is_mobile = false;
foreach ($mobile_agents as $device) {
if (stristr($user_agent, $device)) {
$is_mobile = true;
break;
}
}
return $is_mobile;
}
if(is_mobile()){
echo "手机";
}else{
echo "电脑";
}
复制代码 代码如下:
<?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";
}
?>
代码二:
复制代码 代码如下:
<?php
header("content-type:text/html;charset=utf-8");
function is_mobile(){
$user_agent = $_server['http_user_agent'];
$mobile_agents = array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte");
$is_mobile = false;
foreach ($mobile_agents as $device) {
if (stristr($user_agent, $device)) {
$is_mobile = true;
break;
}
}
return $is_mobile;
}
if(is_mobile()){
echo "手机";
}else{
echo "电脑";
}
上一篇: CAD2017怎么画标准的波浪线?
下一篇: AngularJS实现网站换肤实例