相思资源网 Design By www.200059.com

当用户使用手机等移动终端访问网站时,我们可以通过程序检测用户终端类型,如果是手机用户,则引导用户访问适配手机屏幕的移动站点。本文将介绍分别使用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.jb51.net。

 
if (is_mobile()) { 
  header('Location:http://m.jb51.net'); 
} 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.jb51.net") 
}else{ 
  document.write("请使用手机访问."); 
} 

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

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

标签:
判断访客终端类型

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“判断访客终端类型集锦”

暂无判断访客终端类型集锦的评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。