2017-03-07 15:34:29 8008次浏览 8条回答 2 悬赏 20 金钱

大神好 求yii如何判断是手机访问还是电脑访问

最佳答案

  • 飘逸 发布于 2017-03-10 12:06 举报

    帅哥,不用纠结了,Yii官方是没有提供方法去判断来源是移动设备还是PC的,包括其核心代码以及扩展代码。
    在服务器和客户端的 HTTP 交互中,客户端会通过请求头中的 User-Agent 告知服务器客户端到底是啥玩意。你可以直接获取 这个 User-Agent,不过基于 CGI 协议,PHP可以直接用 $_SERVER['HTTP_USER_AGENT']获取这个 User-Agent。但不幸的是,它是一大堆字符串,人们虽然可能可以通过其直接判断是移动设备还是PC,但程序却很麻烦,毕竟难免有所疏漏之处。所以有专门的人士去做自然最好。
    https://github.com/serbanghita/Mobile-Detect,这个程序可以的。star 六千,还专门为 Yii 写个了扩展,应该不会辱没帅哥你的程序的。。

    7 条回复
    回复于 2017-03-10 16:16 回复

    谢谢 大神 我以为yii自己有这个方法那

    回复于 2017-03-10 18:23 回复

    不用谢,采纳呀,采纳就是最好的感谢。。

    回复于 2017-03-11 08:19 回复

    ok
    大神

    回复于 2017-03-12 15:18 回复

    大神 这个判断手机的方法我如何使用 比如给个成功的例子 或者给个好的教程 我看了下面的教程 我不知道如何做 http://www.yiichina.com/tutorial/594
    这里面的教程似乎改了 我看不懂 大神

    回复于 2017-03-13 12:06 回复

    帅锅,看https://github.com/serbanghita/Mobile-Detect
    ctrl+f 搜索下 yii,看到那个为yii2写的扩展的链接 https://github.com/alexandernst/yii2-device-detect
    然后照做咯。

    回复于 2017-03-13 14:51 回复

    英文不好使实在是伤啊 大神 这样吧 我们以yii2普通版为例 我要把https://github.com/serbanghita/Mobile-Detect 里面的例子下载后 我把它放在那里 之后如何配置 如何调用 如何访问 大神 我小菜求指导或者加我QQ2448640323 大神辛苦了

    回复于 2017-03-13 18:19 回复

    好吧,收人钱财,替人消灾。。

  • 回答于 2017-03-07 15:42 举报
    5 条回复
    回复于 2017-03-07 16:00 回复

    你是百度派来的

    回复于 2017-03-07 16:12 回复

    大神 我是说yii如何判断的

    回复于 2017-03-07 16:20 回复

    这个跟yii没有关系,都是软件底层的玩意儿。还就是判断用户传来的浏览器标识。

    回复于 2017-03-07 16:21 回复

    我是想贴google的链接,怕你们梯子不够长。

    回复于 2017-03-07 18:08 回复

    你们的对话真有意思

  • 回答于 2017-03-08 11:21 举报

    composer下一个

    1 条回复
    回复于 2017-03-08 14:21 回复

    composer下一个 什么意思 求指导

  • 回答于 2017-03-08 14:18 举报

    可以使用 $_SERVER['HTTP_USER_AGENT'] 来进行判断~,然后可以继承 baseModel来封装成一个类中的判断方法~希望可以帮到你

    1 条回复
    回复于 2017-03-08 14:22 回复

    那也不算是 yii的方法把 大神 求指导

  • 回答于 2017-03-08 14:22 举报

    yii框架没有的。 可以用这个 http://www.yiichina.com/tutorial/346

    1 条回复
    回复于 2017-03-08 14:35 回复

    这个我用过 没成功 我用的别的方法 如下面的代码 但yii真的没有吗 还是我们不知道 大神
    <?php

    //手机网页跳转
    //如果检测到访问的浏览器为下列一个指定的移动浏览器 则返回true
    function is_mobile(){
        $regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";
        $regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";    $regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";  
        $regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
        $regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";
        $regex_match.=")/i";      
        return isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));
    }	    
    $is_mobile=is_mobile();
    if($is_mobile){
        //这是一个手机浏览器,可以跳转到手机版网页
        //header("Location: http://www.abc.com/3g");
        echo "手机访问";
      }else{
        //这不是一个手机浏览器
        //header("Location: http://www.abc.com/desktop");
        echo "电脑访问";
      }
    ?>
    
  • 回答于 2018-01-02 22:33 举报

    https://github.com/serbanghita/Mobile-Detect/wiki/Code-examples
    <?php
    // These lines are mandatory.
    require_once 'Mobile_Detect.php';
    $detect = new Mobile_Detect;
    <?php
    // Basic detection.
    $detect->isMobile();
    $detect->isTablet();

    // Magic methods.
    $detect->isIphone();
    $detect->isSamsung();
    // [...]

    // Alternative to magic methods.
    $detect->is('iphone');

    // Find the version of component.
    $detect->version('Android');

    // Additional match method.
    $detect->match('regex.*here');

    // Browser grade method.
    $detect->mobileGrade();

    // Batch methods.
    $detect->setUserAgent($userAgent);
    $detect->setHttpHeaders($httpHeaders);
    <?php
    // Check for mobile environment.
    if ($detect->isMobile()) {

    // Your code here.
    

    }
    <?php
    // Check for tablet device.
    if($detect->isTablet()){

    // Your code here.
    

    }
    <?php
    // Check for any mobile device, excluding tablets.
    if ($detect->isMobile() && !$detect->isTablet()) {

    // Your code here.
    

    }
    <?php
    // Keep the value in $_SESSION for later use
    // and for optimizing the speed of the code.
    if(!$_SESSION['isMobile']){

    $_SESSION['isMobile'] = $detect->isMobile();
    

    }
    <?php
    // Redirect the user to your mobile version of the site.
    if($detect->isMobile()){

    header('http://m.yoursite.com', true, 301);
    

    }
    <?php
    // Include and instantiate the class.
    require_once 'Mobile_Detect.php';
    $detect = new Mobile_Detect;

    // Any mobile device (phones or tablets).
    if ( $detect->isMobile() ) {

    }

    // Any tablet device.
    if( $detect->isTablet() ){

    }

    // Exclude tablets.
    if( $detect->isMobile() && !$detect->isTablet() ){

    }

    // Check for a specific platform with the help of the magic methods:
    if( $detect->isiOS() ){

    }

    if( $detect->isAndroidOS() ){

    }

    // Alternative method is() for checking specific properties.
    // WARNING: this method is in BETA, some keyword properties will change in the future.
    $detect->is('Chrome')
    $detect->is('iOS')
    $detect->is('UCBrowser')
    $detect->is('Opera')
    // [...]

    // Batch mode using setUserAgent():
    $userAgents = array(
    'Mozilla/5.0 (Linux; Android 4.0.4; Desire HD Build/IMM76D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19',
    'BlackBerry7100i/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/103',
    // [...]
    );
    foreach($userAgents as $userAgent){

    $detect->setUserAgent($userAgent);
    $isMobile = $detect->isMobile();
    $isTablet = $detect->isTablet();
    // Use the force however you want.

    }

    // Get the version() of components.
    // WARNING: this method is in BETA, some keyword properties will change in the future.
    $detect->version('iPad'); // 4.3 (float)
    $detect->version('iPhone') // 3.1 (float)
    $detect->version('Android'); // 2.1 (float)
    $detect->version('Opera Mini'); // 5.0 (float)
    // [...]

    1 条回复
    回复于 2018-02-25 15:39 回复

    大神 谢谢

    觉得很赞
  • 回答于 2018-03-08 11:17 举报

    这个tp框架封装了的,直接复制出来用

  • 回答于 2018-07-16 17:29 举报

    自己写了一个方法,方法有点笨,使用了多种判断的方法,不过用起来还算是有用些,可以试一试!

    /**
     * 移动端判断
     * @return bool true/flase
     */
    function isMobile()
    {
        // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
        if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
        {
            return true;
        }
        // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
        if (isset ($_SERVER['HTTP_VIA']))
        {
            // 找不到为flase,否则为true
            return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
        }
        // 脑残法,判断手机发送的客户端标志,兼容性有待提高
        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'
                );
            // 从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') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))))
            {
                return true;
            }
        }
        return false;
    }
    
    觉得很赞
您需要登录后才可以回答。登录 | 立即注册
tansuo
助理

tansuo

注册时间:2017-02-03
最后登录:2018-04-11
在线时长:9小时18分
  • 粉丝4
  • 金钱10
  • 威望0
  • 积分100

热门问题