php如何实现判断是不是手机访问

藏色散人
发布: 2023-03-04 07:18:01
原创
3130 人浏览过

php判断是不是手机访问的实现方法:首先检查是否为wap代理;然后检查浏览器是否接受“WML”;接着通过“preg_match”方法检查“USER_AGENT”即可。

php如何实现判断是不是手机访问

推荐:《PHP视频教程

PHP判断用户是否手机访问

自定义的函数如下:

$agent = check_wap();
    if( $agent )
{
    header('Location: http://www.nowamagic.net');
    exit;
}
// check if wap
function 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框架剥离的判断是否为手机移动终端的函数:

<?php 
 
function is_mobile_request(){  
    $_SERVER[&#39;ALL_HTTP&#39;] = isset($_SERVER[&#39;ALL_HTTP&#39;]) ? $_SERVER[&#39;ALL_HTTP&#39;] : &#39;&#39;;  
    $mobile_browser = &#39;0&#39;;  
    if(preg_match(&#39;/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i&#39;, strtolower($_SERVER[&#39;HTTP_USER_AGENT&#39;])))  
        $mobile_browser++;  
    if((isset($_SERVER[&#39;HTTP_ACCEPT&#39;])) and (strpos(strtolower($_SERVER[&#39;HTTP_ACCEPT&#39;]),&#39;application/vnd.wap.xhtml+xml&#39;) !== false))  
        $mobile_browser++;  
    if(isset($_SERVER[&#39;HTTP_X_WAP_PROFILE&#39;]))  
        $mobile_browser++;  
    if(isset($_SERVER[&#39;HTTP_PROFILE&#39;]))  
        $mobile_browser++;  
    $mobile_ua = strtolower(substr($_SERVER[&#39;HTTP_USER_AGENT&#39;],0,4));  
    $mobile_agents = array(  
        &#39;w3c &#39;,&#39;acs-&#39;,&#39;alav&#39;,&#39;alca&#39;,&#39;amoi&#39;,&#39;audi&#39;,&#39;avan&#39;,&#39;benq&#39;,&#39;bird&#39;,&#39;blac&#39;,  
        &#39;blaz&#39;,&#39;brew&#39;,&#39;cell&#39;,&#39;cldc&#39;,&#39;cmd-&#39;,&#39;dang&#39;,&#39;doco&#39;,&#39;eric&#39;,&#39;hipt&#39;,&#39;inno&#39;,  
        &#39;ipaq&#39;,&#39;java&#39;,&#39;jigs&#39;,&#39;kddi&#39;,&#39;keji&#39;,&#39;leno&#39;,&#39;lg-c&#39;,&#39;lg-d&#39;,&#39;lg-g&#39;,&#39;lge-&#39;,  
        &#39;maui&#39;,&#39;maxo&#39;,&#39;midp&#39;,&#39;mits&#39;,&#39;mmef&#39;,&#39;mobi&#39;,&#39;mot-&#39;,&#39;moto&#39;,&#39;mwbp&#39;,&#39;nec-&#39;,  
        &#39;newt&#39;,&#39;noki&#39;,&#39;oper&#39;,&#39;palm&#39;,&#39;pana&#39;,&#39;pant&#39;,&#39;phil&#39;,&#39;play&#39;,&#39;port&#39;,&#39;prox&#39;,  
        &#39;qwap&#39;,&#39;sage&#39;,&#39;sams&#39;,&#39;sany&#39;,&#39;sch-&#39;,&#39;sec-&#39;,&#39;send&#39;,&#39;seri&#39;,&#39;sgh-&#39;,&#39;shar&#39;,  
        &#39;sie-&#39;,&#39;siem&#39;,&#39;smal&#39;,&#39;smar&#39;,&#39;sony&#39;,&#39;sph-&#39;,&#39;symb&#39;,&#39;t-mo&#39;,&#39;teli&#39;,&#39;tim-&#39;,  
        &#39;tosh&#39;,&#39;tsm-&#39;,&#39;upg1&#39;,&#39;upsi&#39;,&#39;vk-v&#39;,&#39;voda&#39;,&#39;wap-&#39;,&#39;wapa&#39;,&#39;wapi&#39;,&#39;wapp&#39;,  
        &#39;wapr&#39;,&#39;webc&#39;,&#39;winw&#39;,&#39;winw&#39;,&#39;xda&#39;,&#39;xda-&#39; 
        );  
    if(in_array($mobile_ua, $mobile_agents))  
        $mobile_browser++;  
    if(strpos(strtolower($_SERVER[&#39;ALL_HTTP&#39;]), &#39;operamini&#39;) !== false)  
        $mobile_browser++;  
    // Pre-final check to reset everything if the user is on Windows  
    if(strpos(strtolower($_SERVER[&#39;HTTP_USER_AGENT&#39;]), &#39;windows&#39;) !== false)  
        $mobile_browser=0;  
    // But WP7 is also Windows, with a slightly different characteristic  
    if(strpos(strtolower($_SERVER[&#39;HTTP_USER_AGENT&#39;]), &#39;windows phone&#39;) !== false)  
        $mobile_browser++;  
    if($mobile_browser>0)  
        return true;  
    else
        return false;  
}
登录后复制

以上是php如何实现判断是不是手机访问的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板