簡體   English   中英

用PHP檢測移動設備

[英]Detect mobile device in PHP

我有一個滑塊,它顯示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() ){

當我通過移動設備,通過桌面進行視頻時,我該怎么做才能顯示圖片?

我認為最好的方法是通過用戶代理進行檢查。 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;
}

為了檢測移動設備是平板電腦還是手機,我不認為用戶代理有多大幫助。 這是我找到的平板電腦用戶代理列表: 平板電腦用戶代理

試試這個類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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM