您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

php判断用户是否手机访问代码

2024/4/24 11:14:16发布4次查看
之前有一个案例用到判断用户是手机访问还是电脑访问,然后给用户展示相应的页面。最初是用js去做,但是准确度不高,最终采用了php来做,经测试基本上都能判断准
随着移动设备的普及,网站也会迎来越来越多移动设备的访问。用适应pc的页面,很多时候对手机用户不友好,那么有些时候,我们需要判断用户是否用手机访问,如果是手机的话,就跳转到指定的手机友好页面。这里就介绍一下,如何判断用户是否用手机访问。
自定义的函数如下:
$agent = check_wap();if( $agent ){ header('location: '); exit;}// check if wapfunction check_wap(){ // 先检查是否为wap代理,准确度高 if(stristr($_server['http_via'],wap)){return true; } // 检查浏览器是否接受 wml. elseif(strpos(strtoupper($_server['http_accept']),vnd.wap.wml) > 0){return true; } //检查user_agent 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'])){return true;} else{return false; }}
再来一个从php框架剥离的判断是否为手机移动终端的函数:
function is_mobile_request() { $_server['all_http'] = isset($_server['all_http']) ? $_server['all_http'] : ''; $mobile_browser = '0'; if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower($_server['http_user_agent']))) $mobile_browser++; if((isset($_server['http_accept'])) and (strpos(strtolower($_server['http_accept']),'application/vnd.wap.xhtml+xml') !== false)) $mobile_browser++; if(isset($_server['http_x_wap_profile'])) $mobile_browser++; if(isset($_server['http_profile'])) $mobile_browser++; $mobile_ua = strtolower(substr($_server['http_user_agent'],0,4)); $mobile_agents = array('w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac','blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno','ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-','maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-','newt','noki','oper','palm','pana','pant','phil','play','port','prox','qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar','sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-','tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp','wapr','webc','winw','winw','xda','xda-'); if(in_array($mobile_ua, $mobile_agents)) $mobile_browser++; if(strpos(strtolower($_server['all_http']), 'operamini') !== false) $mobile_browser++; // pre-final check to reset everything if the user is on windows if(strpos(strtolower($_server['http_user_agent']), 'windows') !== false) $mobile_browser=0; // but wp7 is also windows, with a slightly different characteristic if(strpos(strtolower($_server['http_user_agent']), 'windows phone') !== false) $mobile_browser++; if($mobile_browser>0) return true; else return false; }
再给大家分享一个个人使用的
function isphone() { // 如果有http_x_wap_profile则一定是移动设备 if (isset($_server['http_x_wap_profile'])) {return true; } //如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息 if (isset($_server['http_via'])) {//找不到为flase,否则为trueif(stristr($_server['http_via'], wap)){return true;} } //脑残法,判断手机发送的客户端标志,兼容性有待提高 if (isset($_server['http_user_agent'])) {$clientkeywords = array ('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile','phone',);// 从http_user_agent中查找手机浏览器的关键字if (preg_match(/( . implode('|', $clientkeywords) . )/i, strtolower($_server['http_user_agent']))) {return true;} } //协议法,因为有可能不准确,,放到最后判断 if (isset($_server['http_accept'])) {// 如果只支持wml并且不支持html那一定是移动设备// 如果支持wml和html但是wml在html之前则是移动设备if ((strpos($_server['http_accept'], 'vnd.wap.wml') !== false) && (strpos($_server['http_accept'], 'text/html') === false || (strpos($_server['http_accept'], 'vnd.wap.wml')
以上所述就是本文的全部内容了,希望大家能够喜欢。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录