判断访客终端类型集锦


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获取input表单值的代码
Apr 19 Javascript
Jquery中使用setInterval和setTimeout的方法
Apr 08 Javascript
javascript和HTML5利用canvas构建猜牌游戏实现算法
Jul 17 Javascript
JavaScript修改css样式style动态改变元素样式
Dec 16 Javascript
获取中文字符串的实际长度代码
Jun 05 Javascript
javascript搜索框效果实现方法
May 14 Javascript
详解JavaScript函数对象
Nov 15 Javascript
Angular.JS中指令ng-if、ng-show/ng-hide和ng-switch的使用教程
May 07 Javascript
JS简单获得节点元素的方法示例
Feb 10 Javascript
vue 实现搜索的结果页面支持全选与取消全选功能
May 10 Javascript
Vue.js实现大转盘抽奖总结及实现思路
Oct 09 Javascript
JS继承实现方法及优缺点详解
Sep 02 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
世界咖啡生产者论坛呼吁:需要立即就咖啡价格采取认真行动
2021/03/06 咖啡文化
php开发过程中关于继承的使用方法分享
2011/06/17 PHP
PHP根据IP地址获取所在城市具体实现
2013/11/27 PHP
php时间戳格式化显示友好的时间函数分享
2014/10/21 PHP
php的curl封装类用法实例
2014/11/07 PHP
PHP中file_put_contents追加和换行的实现方法
2017/04/01 PHP
[原创]php token使用与验证示例【测试可用】
2017/08/30 PHP
限制文本框输入N个字符的js代码
2010/05/13 Javascript
js操作输入框中选择内容兼容IE及其他主流浏览器
2014/04/22 Javascript
如何解决谷歌浏览器下jquery无法获取图片的尺寸
2015/09/10 Javascript
js改变style样式和css样式的简单实例
2016/06/28 Javascript
js实现横向拖拽导航条功能
2017/02/17 Javascript
微信小程序数据存储与取值详解
2018/01/30 Javascript
用p5.js制作烟花特效的示例代码
2018/03/21 Javascript
layui-laydate时间日历控件使用方法详解
2018/11/15 Javascript
localstorage实现带过期时间的缓存功能
2019/06/28 Javascript
JQuery使用属性addClass、removeClass和toggleClass实现增加和删除类操作示例
2019/11/18 jQuery
解决idea开发遇到javascript动态添加html元素时中文乱码的问题
2020/09/29 Javascript
[02:44]DOTA2英雄基础教程 魅惑魔女
2014/01/07 DOTA
[52:00]2018DOTA2亚洲邀请赛 4.1 小组赛 A组加赛 LGD vs Optic
2018/04/02 DOTA
Python Web框架Pylons中使用MongoDB的例子
2013/12/03 Python
python实现Virginia无密钥解密
2019/03/20 Python
用Python写一个自动木马程序
2019/09/17 Python
python爬虫模拟浏览器的两种方法实例分析
2019/12/09 Python
Python集成开发工具Pycharm的安装和使用详解
2020/03/18 Python
Python HTMLTestRunner可视化报告实现过程解析
2020/04/10 Python
mac系统下安装pycharm、永久激活、中文汉化详细教程
2020/11/24 Python
Django 用户认证Auth组件的使用
2020/11/30 Python
函授本科毕业生自我鉴定
2013/10/16 职场文书
学习焦裕禄精神践行三严三实心得体会
2014/10/13 职场文书
优秀班主任主要事迹材料
2014/12/16 职场文书
热血教师观后感
2015/06/10 职场文书
学子宴致辞大全
2015/07/27 职场文书
springboot @ConfigurationProperties和@PropertySource的区别
2021/06/11 Java/Android
JavaScript实现贪吃蛇游戏
2021/06/16 Javascript
Python简易开发之制作计算器
2022/04/28 Python