判断访客终端类型集锦


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 cookies操作集合
Apr 12 Javascript
超酷的网页音乐播放器DewPlayer使用方法
Dec 18 Javascript
node.js中的path.isAbsolute方法使用说明
Dec 08 Javascript
Node.js编程中客户端Session的使用详解
Jun 23 Javascript
jquery实现的缩略图预览滑块实例
Jun 25 Javascript
javascript对象的相关操作小结
May 16 Javascript
Google 地图控件集详解及实例代码
Aug 06 Javascript
AngularJS入门教程之Cookies读写操作示例
Nov 02 Javascript
零基础轻松学JavaScript闭包
Dec 30 Javascript
COM组件中调用JavaScript函数详解及实例
Feb 23 Javascript
30分钟快速入门掌握ES6/ES2015的核心内容(下)
Apr 18 Javascript
实例详解ztree在vue项目中使用并且带有搜索功能
Aug 24 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
博士208HAF收音机实习报告
2021/03/02 无线电
PHP实现检测客户端是否使用代理服务器及其匿名级别
2015/01/07 PHP
php绘图之生成饼状图的方法
2015/01/24 PHP
PHP实现图片上传并压缩
2015/12/22 PHP
Smarty实现页面静态化(生成HTML)的方法
2016/05/23 PHP
Laravel程序架构设计思路之使用动作类
2018/06/07 PHP
php和C#的yield迭代器实现方法对比分析
2019/07/17 PHP
firefo xml 读写实现js代码
2009/06/11 Javascript
JavaScript 字符串乘法
2009/08/20 Javascript
20个非常棒的Jquery实用工具 国外文章
2010/01/01 Javascript
js sort 二维数组排序的用法小结
2014/01/24 Javascript
JavaScript字符串对象substring方法入门实例(用于截取字符串)
2014/10/17 Javascript
JavaScript 动态加载脚本和样式的方法
2015/04/13 Javascript
谷歌Chrome浏览器扩展程序开发小记
2016/01/06 Javascript
Angularjs 滚动加载更多数据
2016/03/17 Javascript
js中window.open的参数及注意注意事项
2016/07/06 Javascript
Bootstrap被封装的弹层
2016/07/20 Javascript
Three.js快速入门教程
2016/09/09 Javascript
Angular.js中用ng-repeat-start实现自定义显示
2016/10/18 Javascript
微信小程序 slider 详解及实例代码
2017/01/10 Javascript
Javascript 详解封装from表单数据为json串进行ajax提交
2017/03/29 Javascript
[01:13]2015国际邀请赛线下观战现场
2015/08/08 DOTA
Python实现生成简单的Makefile文件代码示例
2015/03/10 Python
Python使用random模块生成随机数操作实例详解
2019/09/17 Python
pytorch使用 to 进行类型转换方式
2020/01/08 Python
python中文分词库jieba使用方法详解
2020/02/11 Python
详解Python中list[::-1]的几种用法
2020/11/16 Python
html5 http的轮询和Websocket原理
2018/10/19 HTML / CSS
购买大码女装:Lane Bryant
2016/09/07 全球购物
毕业自我评价范文
2013/11/17 职场文书
公务员培训自我鉴定
2014/02/01 职场文书
护士毕业生自荐信
2014/02/07 职场文书
机关党员三严三实心得体会
2014/10/13 职场文书
2015元旦晚会主持人开场白+结束语
2014/12/14 职场文书
2015年女生节活动总结
2015/02/27 职场文书
幼儿园大班教师随笔
2015/08/14 职场文书