Определение типа устройства и мобильной OS, (Android или IOS)

PHP

13-02-2018

Подключение MobileDetect:

  1. Распаковать файл Mobile_Detect.php на сервер;
  2. Подключить php файл Mobile_Detect.php с помощью директивы require_once
  3. Инициализировать класс:
    <pre class='prettyprint'><code>$detect = new Mobile_Detect; // Инициализируем класс
    </code></pre>
Примеры условий: {ignore}
require_once('путь_к_файлу_на_сервере/Mobile_Detect.php');  // Подключаем скрипт Mobile_Detect
 
$detect = new Mobile_Detect; // Инициализируем класс
 
// Любое мобильное устройство (телефоны или планшеты).
if ( $detect->isMobile() ) {
 
}
 
// Планшеты
if( $detect->isTablet() ){
 
}
 
// Исключаем планшеты
if( $detect->isMobile() && !$detect->isTablet() ){
 
}
 
// Apple IOS
if( $detect->isiOS() ){
 
}
 
// Android OS
if( $detect->isAndroidOS() ){
 
}
 
// Альтернативный метод is() для проверки определенных параметров (БЕТА)
$detect->is('Chrome')
$detect->is('iOS')
$detect->is('UC Browser')
 
// Выборка устройств, отвечающих определенным условиям, используя 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();
 
}
 
// Получаем версии компонентов устройства (БЕТА)
$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)
Сайт проекта: http://mobiledetect.net/
Демо: http://demo.mobiledetect.net
Скачать MobileDetect: https://github.com/serbanghita/Mobile-Detect/
Просмотров: 260