Maison  >  Article  >  développement back-end  >  php如何判断用户通过手机wap访问还是电脑直接访问_PHP教程

php如何判断用户通过手机wap访问还是电脑直接访问_PHP教程

WBOY
WBOYoriginal
2016-07-13 10:44:121149parcourir

现在我们智能手机用户很多了,访问网站也有很多朋友使用手机直接访问,这样存在问题了如果是pc版本手机访问肯定不好看还费人家流量了,所以很多公司做了wap站了,但是要如何判断用户是手机还是pc访问网站呢,下面我整理了一些方法。

最近做一个手机查询系统,自然就牵扯到了此问题,那我就根据对wap的认识浅谈下通过php判断用户访问方式是通过wap访问还是电脑直接访问。
首先说最根本的解决方法:
手机访问时,会附带发送user-agent信息,这个信息里面会有手机号码信息,那么如果能取得手机号码,则可以肯定是通过手机wap访问的。但是目前中国移动已经屏蔽了user-agent信息,所以获取不到手机号码。有关系的朋友可以联系移动公司,把wap网站服务器的ip提交给中国移动,加入白名单后即可取得ua信息。目前中国联通可以直接取到手机号,对联通用户此方案可完美实施。
接下来说我的解决方案:
手机访问,原理是手机通过移动公司的代理服务器进行的访问。那么我们就可以理解是一台普通电脑使用了代理服务器。当手机通过代理服务器访问的时候,http头信息会毫无疑问的包含一个信息:via。这个信息提供了有价值的判断信息。
例如河南移动取得的via信息是:
http/1.1 hazz-b-gw001-wap(infox-wisg, huawei technologies)
河南联通的via信息是:
zxwap gateway,zte technologies
其他各省的http头信息和这个大同小异,判断是否手机访问的方案就出来了:获取http的via信息字符串看是否包含wap字符,如果有则是通过手机访问。这样做的结果是没有人能伪造手机访问,判断绝对准确。自然,这样对于网上流行的手机wap模拟器也做了屏蔽----从根本上屏蔽。
操作代码也很简单:

 代码如下 复制代码
// check if wap by xhat
function check_wap() {
return stristr($_SERVER['HTTP_VIA'],"wap") ? true : false;
}
// check over

从我查阅的资料来看,目前此方法应该是迄今互联网上准确率最高、最简单的判断手机访问方法。

iphone智能手机

为discuz做的iphone版本基本完工,为了方便访问,直接在首页对iphone的访问进行了判断,然后直接跳转

使用以上代码判断,经测试效果很好。

自己用的一个判断类,比较全面了

 代码如下 复制代码

/*
 判断访问用户是否为手机bKjia.c0m
 //判断是否属手机
 */
 function is_mobile() {
  $user_agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  //echo $user_agent;
  $mobile_agents = Array("ipad","wap","android","iphone","sec","sam","ericsson","240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","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","ipaq","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","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte","ben","hai","phili");
  $is_mobile = false;
  foreach ($mobile_agents as $device) {
   if (stristr($user_agent, $device)) {
    if( 'ipad' == $device )
    {
     return $is_mobile;
    }
    $is_mobile = true;
    break;
   }
  }
  return $is_mobile;
 }

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/633111.htmlTechArticle现在我们智能手机用户很多了,访问网站也有很多朋友使用手机直接访问,这样存在问题了如果是pc版本手机访问肯定不好看还费人家流量了...
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn