判断访客终端类型集锦


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 相关文章推荐
Jquery + Ajax调用webService实例代码(asp.net)
Aug 27 Javascript
Chosen 基于jquery的选择框插件使用方法
May 30 Javascript
JavaScript DOM元素尺寸和位置
Apr 13 Javascript
浅析node连接数据库(express+mysql)
Nov 30 Javascript
浅谈JavaScript 函数参数传递到底是值传递还是引用传递
Aug 23 Javascript
bootstrap为水平排列的表单和内联表单设置可选的图标
Feb 15 Javascript
MUI 上拉刷新/下拉加载功能实例代码
Apr 13 Javascript
JavaScript简单实现动态改变HTML内容的方法示例
Dec 25 Javascript
JavaScript的级联函数用法简单示例【链式调用】
Mar 26 Javascript
详解在IDEA中将Echarts引入web两种方式(使用js文件和maven的依赖导入)
Jul 11 Javascript
Element PageHeader页头的使用方法
Jul 26 Javascript
js实现淘宝浏览商品放大镜功能
Oct 28 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下使用以下代码连接并测试
2008/04/09 PHP
php正则表达式获取内容所有链接
2015/07/24 PHP
修复ShopNC使用QQ 互联时提示100010 错误
2015/11/08 PHP
PHP simplexml_import_dom()函数讲解
2019/02/03 PHP
JS遮罩层效果 兼容ie firefox jQuery遮罩层
2010/07/26 Javascript
使用jQuery同时控制四张图片的伸缩实现代码
2013/04/19 Javascript
使用JavaScript判断图片是否加载完成的三种实现方式
2014/05/04 Javascript
JS实现仿京东淘宝竖排二级导航
2014/12/08 Javascript
jQuery链使用指南
2015/01/20 Javascript
jquery实现鼠标滑过显示二级下拉菜单效果
2015/08/24 Javascript
详解原生JavaScript实现jQuery中AJAX处理的方法
2016/05/10 Javascript
JavaScript中的操作符类型转换示例总结
2016/05/30 Javascript
jquery validation验证表单插件
2017/01/07 Javascript
浅谈DOM的操作以及性能优化问题-重绘重排
2017/01/08 Javascript
基于jQuery实现照片墙自动播放特效
2017/01/12 Javascript
JS处理一些简单计算题
2018/02/24 Javascript
手动用webpack搭建第一个ReactApp的示例
2018/04/11 Javascript
ES6箭头函数和扩展实例分析
2020/05/23 Javascript
浅谈vue中使用编辑器vue-quill-editor踩过的坑
2020/08/03 Javascript
Vue实现手机计算器
2020/08/17 Javascript
django自定义Field实现一个字段存储以逗号分隔的字符串
2014/04/27 Python
python写日志封装类实例
2015/06/28 Python
python复制文件到指定目录的实例
2018/04/27 Python
tensorflow 实现数据类型转换
2020/02/17 Python
Python迭代器Iterable判断方法解析
2020/03/16 Python
在PyTorch中使用标签平滑正则化的问题
2020/04/03 Python
django form和field具体方法和属性说明
2020/07/09 Python
Python爬虫爬取有道实现翻译功能
2020/11/27 Python
巴西手表购物网站:eclock
2019/03/19 全球购物
strlen的几种不同实现方法
2013/05/31 面试题
员工晚婚的请假条
2014/02/08 职场文书
2014年小学体育工作总结
2014/12/11 职场文书
大学生学生会工作总结2015
2015/05/26 职场文书
运动会观后感
2015/06/09 职场文书
七年级作文之英语老师
2019/10/28 职场文书
Java十分钟精通进阶适配器模式
2022/04/06 Java/Android