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实例代码如下:
- // check if wap by xhatfunction
- check_wap() {
- return stristr($_SERVER['HTTP_VIA'],"wap") ? true : false;
- }
这样做的结果是没有人能伪造手机访问,判断绝对准确,自然,这样对于网上流行的手机wap模拟器也做了屏蔽—-从根本上屏蔽,这样肯定不行,在网上又看到这一样一个.
php判断手机浏览器的函数,代码如下,加入了对ipad和android的判断:
- // check if wap
- function check_wap(){
- if(stristr($_SERVER['HTTP_VIA'],"wap")){// 先检查是否为wap代理,准确度高
- return true;
- }elseif(strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0){// 检查浏览器是否接受 WML.
- return true;
- }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
- return true;
- }else{
- return false;
- }
- }
测试后还是有问题,不讲你也知道,后来看到了wordpress的 function.php 文件这样一段,代码如下:
- //判断是否属手机
- 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","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;
- }
在你web 加入如下代码:
- if( is_mobile() ){
- include('手机访问.php文件即可');exit;
- }
解决这类问题的方法有很多,如果你不想这样操作可直接使用二级域名了.
Tags: php判断手机访问 实现程序
- 上一篇:PHP数组无限分级数据的层级化处理
- 下一篇:php网页生成验证码实现程序
相关文章
- ·PHP定时执行任务的实现程序(2014-07-27)
- ·ip地址转换成十进制实现程序(2014-07-30)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)