判断访客终端类型集锦


Posted in Javascript onJune 05, 2015

当用户使用手机等移动终端访问网站时,我们可以通过程序检测用户终端类型,如果是手机用户,则引导用户访问适配手机屏幕的移动站点。本文将介绍分别使用PHP和JAVASCRIPT代码判断用户终端类型。

PHP版

我们使用PHP的$_SERVER['HTTP_USER_AGENT']来获取手机用户浏览器的用户代理,然后匹配已有的各种手机浏览器代理库,如果含有匹配的关键字,则判断为手机(移动终端)用户。

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","iphone","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; 
}

上述代码中函数is_mobile()来判断用户终端类型,将收集到的当今各种手机的HTTP_USER_AGENT归结到数组$mobile_agents中,并进行匹配。使用时只需调用函数is_mobile()。如以下代码表示,当匹配用户为手机访问时,页面跳转到网站手机版m.3water.com。

if (is_mobile()) { 
  header('Location:http://m.3water.com'); 
} else { 
  echo '请使用手机访问.'; 
}

Javascript版

您也可以直接在前端页面上加入一段Javascript脚本来判断用户的终端类型。Javascript也是通过获取浏览器的user-agent信息,然后匹配已有的user-agent信息库。

if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iOS|iPad|Backerry| 
WebOS|Symbian|Windows Phone|Phone)/i))) { 
  location.replace("http://m.3water.com") 
}else{ 
  document.write("请使用手机访问."); 
}

以上代码还不是很完善,有兴趣的朋友欢迎补充。

当然,我们也可以通过响应式布局来匹配各种不同屏幕,这样可以节约开发成本,但是当客户对移动网站的功能需求,对于独立的移动站点最好是采用在网站入口处判断用户访问终端类型,一般是我们在主站首页就做判断,如果是手机访客则跳转到手机版页面,否则按正常PC方式访问页面。

Javascript 相关文章推荐
原创javascript小游戏实现代码
Aug 19 Javascript
使用apply方法处理数组的三个技巧[译]
Sep 20 Javascript
利用javascript实现禁用网页上所有文本框,下拉菜单,多行文本域
Dec 14 Javascript
html文本框提示效果的示例代码
Jun 28 Javascript
jQuery预加载图片常用方法
Jun 15 Javascript
JavaScript运行过程中的“预编译阶段”和“执行阶段”
Dec 16 Javascript
详细谈谈javascript的对象
Jul 31 Javascript
AngularJS表格详解及示例代码
Aug 17 Javascript
js中Object.defineProperty()方法的不详解
Jul 09 Javascript
JS使用Dijkstra算法求解最短路径
Jan 17 Javascript
js array数组对象操作方法汇总
Mar 18 Javascript
js实现批量删除功能
Aug 27 Javascript
JavaScript中constructor()方法的使用简介
Jun 05 #Javascript
JavaScript中的原型prototype属性使用详解
Jun 05 #Javascript
HTML5使用DeviceOrientation实现摇一摇功能
Jun 05 #Javascript
简介JavaScript中POSITIVE_INFINITY值的使用
Jun 05 #Javascript
JavaScript中Number.NEGATIVE_INFINITY值的使用详解
Jun 05 #Javascript
jQuery+HTML5实现手机摇一摇换衣特效
Jun 05 #Javascript
在JavaScript中使用NaN值的方法
Jun 05 #Javascript
You might like
php写的AES加密解密类分享
2014/06/20 PHP
Zend Framework教程之Application用法实例详解
2016/03/14 PHP
PHP7.0连接DB操作实例分析【基于mysqli】
2019/09/26 PHP
完美解决JS中汉字显示乱码问题(已解决)
2006/12/27 Javascript
javascript 设置某DIV区域内的checkbox复选框
2009/11/30 Javascript
基于jquery的一个图片hover的插件
2010/04/24 Javascript
可以用鼠标拖动的DIV实现思路及代码
2013/10/21 Javascript
jQuery 追加元素的方法如append、prepend、before
2014/01/16 Javascript
原生javascript实现自动更新的时间日期
2016/02/12 Javascript
让图片跳跃起来  javascript图片轮播特效
2016/02/16 Javascript
JS复制对应id的内容到粘贴板(Ctrl+C效果)
2017/01/23 Javascript
React-Native做一个文本输入框组件的实现代码
2017/08/10 Javascript
jQuery实现广告条滚动效果
2017/08/22 jQuery
vue.js默认路由不加载linkActiveClass问题的解决方法
2017/12/11 Javascript
关于Google发布的JavaScript代码规范你要知道哪些
2018/04/04 Javascript
Angular PWA使用的Demo示例
2019/01/31 Javascript
vue实现点击按钮“查看详情”弹窗展示详情列表操作
2020/09/09 Javascript
[02:50]【扭转乾坤,只此一招】DOTA2全新版本永雾林渊开启新篇章
2020/12/24 DOTA
详解Python验证码识别
2016/01/25 Python
python3制作捧腹网段子页爬虫
2017/02/12 Python
python中如何使用正则表达式的集合字符示例
2017/10/09 Python
基于numpy中数组元素的切片复制方法
2018/11/15 Python
使用python批量修改文件名的方法(视频合并时)
2020/03/24 Python
pytorch获取vgg16-feature层输出的例子
2019/08/20 Python
为什么黑客都用python(123个黑客必备的Python工具)
2020/01/31 Python
请用用Java代码写一个堆栈
2012/01/26 面试题
校外活动方案
2014/08/28 职场文书
学校青年志愿者活动总结
2015/05/06 职场文书
员工福利申请报告
2015/05/15 职场文书
消费者理赔投诉书
2015/07/02 职场文书
女方家长婚礼致辞
2015/07/27 职场文书
化工厂员工工作总结
2015/10/15 职场文书
高一军训口号
2015/12/25 职场文书
python文件目录操作之os模块
2021/05/08 Python
python数据可视化JupyterLab实用扩展程序Mito
2021/11/20 Python
Mysql 文件配置解析介绍
2022/05/06 MySQL