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


Posted in PHP onApril 24, 2013

代码一:

<?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 相关文章推荐
亲密接触PHP之PHP语法学习笔记1
Dec 17 PHP
PHP获取url的函数代码
Aug 02 PHP
PHP 文本文章分页代码 按标记或长度(不涉及数据库)
Jun 07 PHP
深入PHP异步执行的详解
Jun 03 PHP
php curl获取网页内容(IPV6下超时)的解决办法
Jul 16 PHP
php查找任何页面上的所有链接的方法
Dec 03 PHP
PHP5.3与5.5废弃与过期函数整理汇总
Jul 10 PHP
PHP图片库imagemagick安装方法
Sep 23 PHP
thinkphp视图模型查询提示ERR: 1146:Table 'db.pr_order_view' doesn't exist的解决方法
Oct 30 PHP
php使用APC实现实时上传进度条功能
Oct 26 PHP
PHP中ID设置自增后不连续的原因分析及解决办法
Aug 21 PHP
php传值和传引用的区别点总结
Nov 19 PHP
用PHP提取中英文词语以及数字的首字母的方法介绍
Apr 23 #PHP
基于PHP读取TXT文件向数据库导入海量数据的方法
Apr 23 #PHP
『PHP』PHP截断函数mb_substr()使用介绍
Apr 22 #PHP
基于magic_quotes_gpc与magic_quotes_runtime的区别与使用介绍
Apr 22 #PHP
怎样给PHP源代码加密?PHP二进制加密与解密的解决办法
Apr 22 #PHP
基于PHP Web开发MVC框架的Smarty使用说明
Apr 19 #PHP
PHP时间戳与日期之间转换的实例介绍
Apr 19 #PHP
You might like
随时给自己贴的图片加文字的php代码
2007/03/08 PHP
php将html转成wml的WAP标记语言实例
2015/07/08 PHP
PHP 中TP5 Request 请求对象的实例详解
2017/07/31 PHP
浅谈php的TS和NTS的区别
2019/03/13 PHP
Mootools 1.2教程 设置和获取样式表属性
2009/09/15 Javascript
JQUERY操作JSON实例代码
2010/02/09 Javascript
JQuery扩展插件Validate 3通过参数设置错误信息
2011/09/05 Javascript
JavaScript检测上传文件大小的方法
2015/07/22 Javascript
谈谈AngularJs中的隐藏和显示
2015/12/09 Javascript
在JavaScript中使用JSON数据
2016/02/15 Javascript
快速解决vue-cli不能初始化webpack模板的问题
2018/03/20 Javascript
angular.js实现列表orderby排序的方法
2018/10/02 Javascript
[14:57]DOTA2 HEROS教学视频教你分分钟做大人-幽鬼
2014/06/13 DOTA
[02:47]DOTA2亚洲邀请赛 HR战队出场宣传片
2015/02/07 DOTA
[52:40]完美世界DOTA2联赛PWL S2 Magma vs GXR 第一场 11.29
2020/12/02 DOTA
python返回昨天日期的方法
2015/05/13 Python
Python常用小技巧总结
2015/06/01 Python
Python爬虫实现网页信息抓取功能示例【URL与正则模块】
2017/05/18 Python
Python编程实现及时获取新邮件的方法示例
2017/08/10 Python
Python celery原理及运行流程解析
2020/06/13 Python
python中rc1什么意思
2020/06/19 Python
详解python 内存优化
2020/08/17 Python
用Python自动清理系统垃圾的实现
2021/01/18 Python
Mio Skincare美国官网:身体紧致及孕期身体护理
2017/03/05 全球购物
风险评估实施方案
2014/03/09 职场文书
企业宣传策划方案
2014/05/29 职场文书
经济贸易系毕业生求职信
2014/05/31 职场文书
电子信息专业应届生自荐信
2014/06/04 职场文书
学习十八大演讲稿
2014/09/15 职场文书
计算机实训报告总结
2014/11/05 职场文书
借款民事起诉状范文
2015/05/19 职场文书
辩论赛主持人开场白
2015/05/29 职场文书
薪资证明范本
2015/06/19 职场文书
环保宣传语大全
2015/07/13 职场文书
人事任命书范本
2015/09/21 职场文书
建立共青团委员会的请示
2019/04/02 职场文书