• PHP和javascript判断访客终端是电脑还是手机


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

    PHP版

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

     1 function is_mobile() { 
     2     $user_agent = $_SERVER['HTTP_USER_AGENT']; 
     3     $mobile_agents = array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi", 
     4     "android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio", 
     5     "au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu", 
     6     "cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ", 
     7     "fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi", 
     8     "htc","huawei","hutchison","inno","ipad","ipaq","iphone","ipod","jbrowser","kddi", 
     9     "kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo", 
    10     "mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-", 
    11     "moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia", 
    12     "nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-", 
    13     "playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo", 
    14     "samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank", 
    15     "sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit", 
    16     "tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin", 
    17     "vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce", 
    18     "wireless","xda","xde","zte"); 
    19     $is_mobile = false; 
    20     foreach ($mobile_agents as $device) { 
    21         if (stristr($user_agent, $device)) { 
    22             $is_mobile = true; 
    23             break; 
    24         } 
    25     } 
    26     return $is_mobile; 
    27 } 

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

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

    Javascript版

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

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

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

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

  • 相关阅读:
    Cocos2d-js 开发记录:图片数据资源等的异步加载
    Cocos2d-js 开发记录:声音播放
    Cocos2d-js 开发记录-初始
    PAT 1064 Complete Binary Search Tree
    python 对象属性与 getattr & setattr
    LeetCode Text Justification
    LeetCode Valid Number
    LeetCode String to Integer (atoi)
    struts2--标签取值
    java--Hibernate实现分页查询
  • 原文地址:https://www.cnblogs.com/joyco773/p/5438552.html
Copyright © 2020-2023  润新知