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编程开发“虚拟域名”系统
Oct 09 PHP
解析CI即CodeIgniter框架在Nginx下的重写规则
Jun 03 PHP
php数组去重实例及分析
Nov 26 PHP
ECSHOP在PHP5.5及高版本上报错的解决方法
Aug 31 PHP
PHP 实现的将图片转换为TXT
Oct 21 PHP
PHP的数组中提高元素查找与元素去重的效率的技巧解析
Mar 03 PHP
php is_writable判断文件是否可写实例代码
Oct 13 PHP
php获取flash尺寸详细数据的方法
Nov 12 PHP
PHP实现广度优先搜索算法(BFS,Broad First Search)详解
Sep 16 PHP
ThinkPHP5.0框架使用build 自动生成模块操作示例
Apr 11 PHP
PHP使用Session实现上传进度功能详解
Aug 06 PHP
浅析PHP反序列化中过滤函数使用不当导致的对象注入问题
Feb 15 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
咖啡风味 世界咖啡主要分布分布 咖啡的生长要求
2021/03/06 新手入门
解析php session_set_save_handler 函数的用法(mysql)
2013/06/29 PHP
请离开include_once和require_once
2013/07/18 PHP
php获取qq用户昵称和在线状态(实例分析)
2013/10/27 PHP
php对二维数组按指定键值key排序示例代码
2013/11/26 PHP
php阿拉伯数字转中文人民币大写
2015/12/21 PHP
JavaScript 学习 - 提高篇
2007/02/02 Javascript
JavaScript中的Location地址对象
2008/01/16 Javascript
jQuery 阴影插件代码分享
2012/01/09 Javascript
一行命令搞定node.js 版本升级
2014/07/20 Javascript
js闭包所用的场合以及优缺点分析
2015/06/22 Javascript
Jquery attr()方法 属性赋值和属性获取详解
2016/04/15 Javascript
javascript淘宝主图放大镜功能
2016/10/20 Javascript
AngularJS递归指令实现Tree View效果示例
2016/11/07 Javascript
Angular.Js的自动化测试详解
2016/12/09 Javascript
纯js实现动态时间显示
2020/09/07 Javascript
JS实现简单的选择题测评系统代码思路详解(demo)
2017/09/03 Javascript
node 利用进程通信实现Cluster共享内存
2017/10/27 Javascript
如何在Vue.js中实现标签页组件详解
2019/01/02 Javascript
微信小程序实现上传word、txt、Excel、PPT等文件功能
2019/05/23 Javascript
发布订阅模式在vue中的实际运用实例详解
2019/06/09 Javascript
解决vue打包后刷新页面报错:Unexpected token
2019/08/27 Javascript
详解ES6数组方法find()、findIndex()的总结
2020/05/12 Javascript
jquery实现有过渡效果的tab切换
2020/07/17 jQuery
通过JS判断网页是否为手机打开
2020/10/28 Javascript
TensorFlow卷积神经网络之使用训练好的模型识别猫狗图片
2019/03/14 Python
python标准库sys和OS的函数使用方法与实例详解
2020/02/12 Python
解决paramiko执行命令超时的问题
2020/04/16 Python
聊聊python在linux下与windows下导入模块的区别说明
2021/03/03 Python
iRobot官网:改变生活的家用机器人品牌
2016/09/20 全球购物
土木工程毕业生自荐信
2013/11/12 职场文书
优秀班集体申报材料
2014/12/25 职场文书
2015年工程师工作总结
2015/04/30 职场文书
亮剑精神观后感
2015/06/05 职场文书
幼儿园教师读书笔记
2015/06/29 职场文书
护士爱岗敬业心得体会
2016/01/25 职场文书