【问题标题】:find mobile browser by function按功能查找手机浏览器
【发布时间】:2015-04-20 06:49:44
【问题描述】:

我有一个运行良好的函数,但我需要对其进行一些扩展,而且我不是程序员。

现在它可以很好地检测到ios,android和web都重定向到一个页面,web和android。

目标是,将 ios 重定向到第 1 页,将 android 重定向到第 2 页,将 web-os 重定向到第 3 页。

 function find_mobile_browser() {
    if(preg_match('/(iphone|ipad|ipod)/i', $_SERVER['HTTP_USER_AGENT'])) {
        return true;
    } else {
        return false;
    }
}

电话是这样的:

<?php $mobile_browser = find_mobile_browser(); 
                        if($mobile_browser) { 
                        include("ios.php"); /* if mobile browser detected, do this */ 
                        } else { 
                        include("android_and_web.php"); /* else do this */ 
                        }
                        ?>

你能帮我扩展这个 sn-p 来检测它吗,这样调用就可以了:

<?php $mobile_browser = find_mobile_browser(); 
                        if($mobile_browser) { 
                        include("ios.php"); /* if ios detected, do this */ 
                        } else { 
                        include("android.php"); /* if android detected, do this */ 
                        }

                        } else { 
                        include("web_os.php"); /* if web-os detected, do this */ 
                        }
                        ?>

我认为这不是检测移动设备的完美方法,但有更好的方法吗?

谢谢, 奥威尔

在你的好答案之后,我会解释得更好。

“标题”答案对我很有用,但我不是程序员,所以我不能让它按我想要的方式工作。重定向效果很好,但我不需要重定向,我需要包含。我想在我的一些模板中包含依赖于 ios/android/web 的一些文本 sn-p(php 文件)。

该函数运行良好,但我的调用没有显示任何结果。在这里,我尝试调用该函数:

    <?php
    $mobile_browser = find_mobile_browser();

    if($mobile_browser == 'ios')
    {
        include("ios.php");
    }
    elseif ($mobile_browser == 'android')
    {
        include("android.php");
    }
    else
    {
        /* if no mobile os detected, include "web.php" */
    }

?>

希望我现在更清楚了,你不要难过。

提前致谢, 奥威尔

感谢您的精彩回答,-我现在明白了 :))

干杯, 奥威尔

【问题讨论】:

标签: php browser-detection


【解决方案1】:

使用Mobile-Detect php 类。

例子:

<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;

if($detect->isAndroidOS()) {
 include("android.php");
}elseif( $detect->isiOS()) {
 include("ios.php");
}else( $detect->iswebOS()) {
 include("web_os.php");
}
?>

【讨论】:

    【解决方案2】:

    为了重定向,您可以使用header

    header('Location:'.$your_url);
    

    所以代码会变成这样(取决于$mobile_browser 的值)

    function find_mobile_browser()
    {
        if(preg_match('/(iphone|ipad|ipod)/i', $_SERVER['HTTP_USER_AGENT']))
        {
            return 'ios';
        }
        elseif (preg_match('/(android)/i', $_SERVER['HTTP_USER_AGENT']))
        {
            return 'android';
        }
        else
        {
            return false;
        }
    }
    
    <?php
        $mobile_browser = find_mobile_browser();
    
        $ios_url = 'http://www.example.com';
        $android_url = 'http://www.example.com';
        $web_url = 'http://www.example.com';
    
        if($mobile_browser == 'ios')
        {
            header('Location:'.$ios_url);
            exit;
        }
        elseif ($mobile_browser == 'android')
        {
            header('Location:'.$android_url);
            exit;
        }
        else
        {
            header('Location:'.$web_url);
            exit;
        }
    

    ?>

    【讨论】:

    • find_mobile_browser() 是否返回 androidios ?我不这么认为。另外,如果在使用Location: 之后,为什么还要使用including 文件?抱歉,您的代码没有任何意义。
    • @PedroLobito 好的,立即查看更新:添加了扩展的 sn-p 并删除了包含。
    【解决方案3】:

    您可以使用名为:Mobile Detect的 PHP 类

    下载链接:https://github.com/serbanghita/Mobile-Detect/archive/2.8.12.zip

    这是使用它的示例 sn-p,您可以根据需要对其进行自定义:

    <?php
    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() ){
    
    }
    
    $detect->is('Chrome');
    $detect->is('iOS');
    $detect->is('UC Browser');
    
    $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();
    
    }
    
    $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)
    
    ?>
    

    【讨论】:

    • 这是一个类而不是一个库;)
    • 感谢您提及.. :) @PedroLobito
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    • 2015-03-17
    相关资源
    最近更新 更多