当前位置:首页 > PHP教程 > php应用 > 列表

php判断手机访问实现程序

发布:smiling 来源: PHP粉丝网  添加日期:2014-07-30 11:14:39 浏览: 评论:0 

因为工作需我们写一个电脑手机共用一个域名,当是手机用户是就访问手机页面,如果是电脑用户就访问web页面,下面我们找到判断为手机用户php代码分享给大家.

网上找到的:手机访问,原理是手机通过移动公司的代理服务器进行的访问,那么我们就可以理解是一台普通电脑使用了代理服务器,当手机通过代理服务器访问的时候,http头信息会毫无疑问的包含一个信息:via,这个信息提供了有价值的判断信息.

例如河南移动取得的via信息是:

http/1.1 hazz-b-gw001-wap(infox-wisg, huawei technologies)

河南联通的via信息是:zxwap gateway,zte technologies

PHP实例代码如下:

  1. // check if wap by xhatfunction  
  2. check_wap() {  
  3.  return stristr($_SERVER['HTTP_VIA'],"wap") ? true : false; 

这样做的结果是没有人能伪造手机访问,判断绝对准确,自然,这样对于网上流行的手机wap模拟器也做了屏蔽—-从根本上屏蔽,这样肯定不行,在网上又看到这一样一个.

php判断手机浏览器的函数,代码如下,加入了对ipad和android的判断:

  1. // check if wap  
  2. function check_wap(){ 
  3.  if(stristr($_SERVER['HTTP_VIA'],"wap")){// 先检查是否为wap代理,准确度高 
  4.   return true
  5.  }elseif(strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0){// 检查浏览器是否接受 WML. 
  6.   return true
  7.    }elseif(preg_match('/(blackberry|configuration/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])){//检查USER_AGENT 
  8.   return true;     
  9.  }else
  10.   return false;  
  11.    } 

测试后还是有问题,不讲你也知道,后来看到了wordpress的 function.php 文件这样一段,代码如下:

  1. //判断是否属手机 
  2. function is_mobile() { 
  3.  $user_agent = $_SERVER['HTTP_USER_AGENT']; 
  4.  $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","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"); 
  5.  $is_mobile = false; 
  6.  foreach ($mobile_agents as $device) { 
  7.   if (stristr($user_agent$device)) { 
  8.    $is_mobile = true; 
  9.    break
  10.   } 
  11.  } 
  12.  return $is_mobile

在你web 加入如下代码:

  1. if( is_mobile() ){ 
  2.   include('手机访问.php文件即可');exit

解决这类问题的方法有很多,如果你不想这样操作可直接使用二级域名了.

Tags: php判断手机访问 实现程序

分享到: