首页 > 资讯列表 > 编程/数据库 >> PHP

php判断终端是手机还是电脑访问网站的思路及示例代码

PHP 2014-12-21 20:03:05 转载来源: 网络整理/侵权必删

本文为大家讲解的是php中如何判断终端是手机还是电脑访问网站的思路及示例代码,主要是通过浏览器的ua信息来判别,感兴趣的同学参考下。 代码一: <?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 {

本文为大家讲解的是php中如何判断终端手机还是电脑访问站的思路及示例代码,主要是通过浏览器的ua信息来判别,感兴趣的同学参考下。


代码一:


<?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 判断 终端 手机 还是 电脑 访问 站的 思路


声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!

站长搜索

http://www.adminso.com

Copyright @ 2007~2024 All Rights Reserved.

Powered By 站长搜索

打开手机扫描上面的二维码打开手机版


使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

站长搜索目录系统技术支持