【问题标题】:Detect mobile device in PHP在 PHP 中检测移动设备
【发布时间】:2019-04-10 06:09:15
【问题描述】:

我有一个滑块,它显示4个视频,我需要在移动设备时显示图片,通过桌面时显示视频,滑块写在main.min.js,并包含在main.tpl(模板),我有一个检测移动设备的脚本

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

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

}

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

移动设备显示图片,桌面显示视频?

【问题讨论】:

  • 你试过这个解决方案了吗? stackoverflow.com/a/4117597/4459647
  • 您应该使用 php 来确定用户设备,因为 php 是一种服务器端语言,所以它不是一个好方法。您可以轻松地使用 CSS 和/或 JS/jQuery 来识别用户设备,因为它们在客户端工作,
  • @Vishwa 在服务器端检测设备有什么问题?了解设备后,您可以为特定设备提供自定义版本的页面。
  • @vishwa 那么您建议哪种方式?我应该写什么

标签: javascript php html css templates


【解决方案1】:

我认为最好的方法是通过用户代理进行检查。 WordPress 的核心有一个 wp_is_mobile 功能,可能会有所帮助(我已经删除了它的 WP 部分):

function is_mobile() {
    if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
        $is_mobile = false;
    } elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Mobile' ) !== false // many mobile devices (all iPhone, iPad, etc.)
        || strpos( $_SERVER['HTTP_USER_AGENT'], 'Android' ) !== false
        || strpos( $_SERVER['HTTP_USER_AGENT'], 'Silk/' ) !== false
        || strpos( $_SERVER['HTTP_USER_AGENT'], 'Kindle' ) !== false
        || strpos( $_SERVER['HTTP_USER_AGENT'], 'BlackBerry' ) !== false
        || strpos( $_SERVER['HTTP_USER_AGENT'], 'Opera Mini' ) !== false
        || strpos( $_SERVER['HTTP_USER_AGENT'], 'Opera Mobi' ) !== false ) {
            $is_mobile = true;
    } else {
        $is_mobile = false;
    }
    return $is_mobile;
}

对于检测移动设备是平板电脑还是手机,我认为用户代理没有多大帮助。这是我找到的平板电脑用户代理列表:Tablet User Agents

【讨论】:

    【解决方案2】:

    试试这个类http://mobiledetect.net,它最适合检测不同的设备。

    // Include and instantiate the class.
    require_once 'Mobile_Detect.php';
    $detect = new Mobile_Detect;
    
    // Any mobile device (phones or tablets).
    if ( $detect->isMobile() ) {
        // Show the image here for mobile
    }
    
    if ( !$detect->isMobile() ) {
        // Show the image here for computer
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-06
      • 2010-11-20
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多