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 相关文章推荐
解决phpmyadmin 乱码,支持gb2312和utf-8
Nov 20 PHP
实现了一个PHP5的getter/setter基类的代码
Feb 25 PHP
php file_get_contents函数轻松采集html数据
Apr 22 PHP
PHP学习之数组值的操作
Apr 17 PHP
php在项目中寻找代码的坏味道(综艺命名)
Jul 19 PHP
深入解析PHP的引用计数机制
Jun 14 PHP
解析wamp5下虚拟机配置文档
Jun 27 PHP
PHP验证码函数代码(简单实用)
Sep 29 PHP
php实现快速排序的三种方法分享
Mar 12 PHP
PHP魔术引号所带来的安全问题分析
Jul 15 PHP
PHP魔术方法的使用示例
Jun 23 PHP
php求数组全排列,元素所有组合的方法总结
Mar 14 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:风雨欲来 路在何方?
2006/10/09 PHP
laravel框架中间件 except 和 only 的用法示例
2019/07/12 PHP
ThinkPHP3.1.2 使用cli命令行模式运行的方法
2020/04/14 PHP
Jquery替换已存在于element上的event的方法
2010/03/09 Javascript
为JavaScript类型增加方法的实现代码(增加功能)
2011/12/29 Javascript
js导出table到excel同时兼容FF和IE示例
2013/09/03 Javascript
JQuery悬停控制图片轮播——代码简单
2015/08/05 Javascript
JS实现带提示的星级评分效果完整实例
2015/10/30 Javascript
初步了解javascript面向对象
2015/11/09 Javascript
JavaScript获取css行间样式,内连样式和外链样式的简单方法
2016/07/18 Javascript
利用Js+Css实现折纸动态导航效果实例源码
2017/01/25 Javascript
vue实现手机号码抽奖上下滚动动画示例
2017/10/18 Javascript
Vue-cli-webpack搭建斗鱼直播步骤详解
2017/11/17 Javascript
easyui下拉框动态级联加载的示例代码
2017/11/29 Javascript
[34:44]Liquid vs TNC Supermajor 胜者组 BO3 第二场 6.4
2018/06/05 DOTA
python3使用urllib示例取googletranslate(谷歌翻译)
2014/01/23 Python
python简单实现基于SSL的IRC bot实例
2015/06/15 Python
Odoo中如何生成唯一不重复的序列号详解
2018/02/10 Python
详解Python Qt的窗体开发的基本操作
2019/07/14 Python
Python字符串处理的8招秘籍(小结)
2019/08/13 Python
Django中的FBV和CBV用法详解
2019/09/15 Python
python十进制转二进制的详解
2020/02/07 Python
Python中if有多个条件处理方法
2020/02/26 Python
python ssh 执行shell命令的示例
2020/09/29 Python
python 如何引入协程和原理分析
2020/11/30 Python
css3media响应式布局实例
2016/07/08 HTML / CSS
感恩教育活动总结
2014/05/05 职场文书
中学生教师节演讲稿
2014/09/03 职场文书
关于国庆节的演讲稿
2014/09/05 职场文书
党的群众路线教育实践活动对照检查材料
2014/09/22 职场文书
十佳少年事迹材料
2014/12/25 职场文书
2015年依法治校工作总结
2015/07/27 职场文书
高一军训感想
2015/08/07 职场文书
商务信函英语问候语
2015/11/10 职场文书
幼师自荐信范文(2016推荐篇)
2016/01/28 职场文书
Redis延迟队列和分布式延迟队列的简答实现
2021/05/13 Redis