• php判断页面访问是移动端还是pc端


    在使用浏览器发起的 HTTP 请求中,通常会包含一个识别标识。它名为 User Agent,简称 UA。它是一串包含了客户端基础信息的字符串。通过它可以方便的获取客户端的操作系统,语言,浏览器和版本信息。

    1、获取UA标识

    echo $_SERVER['HTTP_USER_AGENT'];

      结果:

    Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36

      根据得到的字符串匹配出访问方式 但是手机系统很多种 

    2、封装方法进行判断

    if (!function_exists("isMobile")) {
        function isMobile()
        {
            $user_agent = $_SERVER['HTTP_USER_AGENT'];
            $mobile_agents = array ('iphone','android','phone','mobile','wap','netfront','java','opera mobi',
                'opera mini','ucweb','windows ce','symbian','series','webos','sony','blackberry','dopod',
                'nokia','samsung','palmsource','xda','pieplus','meizu','midp','cldc','motorola','foma',
                'docomo','up.browser','up.link','blazer','helio','hosin','huawei','novarra','coolpad',
                'techfaith','alcatel','amoi','ktouch','nexian','ericsson','philips','sagem','wellcom',
                'bunjalloo','maui','smartphone','iemobile','spice','bird','zte-','longcos','pantech',
                'gionee','portalmmm','jig browser','hiptop','benq','haier','^lct','320x320','240x320',
                '176x220','windows phone','cect','compal','ctl','lg','nec','tcl','daxian','dbtel','eastcom',
                'konka','kejian','lenovo','mot','soutec','sgh','sed','capitel','panasonic','sonyericsson',
                'sharp','panda','zte','acer','acoon','acs-','abacho','ahong','airness','anywhereyougo.com',
                'applewebkit/525','applewebkit/532','asus','audio','au-mic','avantogo','becker','bilbo',
                'bleu','cdm-','danger','elaine','eric','etouch','fly ','fly_','fly-','go.web','goodaccess',
                'gradiente','grundig','hedy','hitachi','htc','hutchison','inno','ipad','ipaq','ipod',
                'jbrowser','kddi','kgt','kwc','lg ','lg2','lg3','lg4','lg5','lg7','lg8','lg9','lg-','lge-',
                'lge9','maemo','mercator','meridian','micromax','mini','mitsu','mmm','mmp','mobi','mot-',
                'moto','nec-','newgen','nf-browser','nintendo','nitro','nook','obigo','palm','pg-',
                'playstation','pocket','pt-','qc-','qtek','rover','sama','samu','sanyo','sch-','scooter',
                'sec-','sendo','sgh-','siemens','sie-','softbank','sprint','spv','tablet','talkabout',
                'tcl-','teleca','telit','tianyu','tim-','toshiba','tsm','utec','utstar','verykool','virgin',
                'vk-','voda','voxtel','vx','wellco','wig browser','wii','wireless','xde','pad','gt-p1000');
            $ismobile = false;
            foreach ($mobile_agents as $device) {
                if (stristr($user_agent, $device)) {
                    $ismobile = true;
                    break;
                }
            }
            return $ismobile;
        }
    }

     这样直接判断方法的返回值就可以直接判断是pc还是移动

  • 相关阅读:
    cd的使用
    转换器模式
    装饰模式
    策略模式
    模板方法模式
    工厂模式
    类型信息
    proto编译组件使用
    proto编译引用外部包问题
    Kafka经典三大问:数据有序丢失重复
  • 原文地址:https://www.cnblogs.com/yaradish/p/10479726.html
Copyright © 2020-2023  润新知