Skip to content

Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.

License

Notifications You must be signed in to change notification settings

catcoder/Mobile-Detect

 
 

Repository files navigation

> Motto: "Every business should have a mobile detection script to detect mobile readers."

Coverage Status Build Status Scrutinizer Code Quality Minimum PHP Version

Installing

Using composer.

Getting started

When you want to detect information about a device, you simply need to detect it.

$mobileDetect = new MobileDetect();
$device = $mobileDetect->detect();

if ($device->isMobile()) {
  // hooray!
}

Once you've detected your device, you have an array of methods available to you to retrieve various properties about your device, such as browser, operating system, vendor, model, and any version for these or any other properties.

echo "You are using " . $device->getBrowser() . " browser with version "
  . " with version " . $device->getBrowserVersion();

Let's see all the methods in action:

$ua = 'Mozilla/5.0 (Linux; U; Android 4.0.4; ru-ru; GT-P5100 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30';
$mobileDetect = new MobileDetect($ua);
$device = $mobileDetect->detect();
$device->isMobile(); // (bool) true
$device->isTablet(); // (bool) true
$device->getBrowser() // (string) 'Safari Mobile'
$device->getBrowserVersion() // (string) '4.0'
$device->getOperatingSystem() // (string) 'Android'
$device->getOperatingSystemVersion() // (string) '4.0.4'
$device->getModel() // (string) 'GT-P5100'

You may also be looking for a very specific version in your device:

echo "You are using WebKit version: " . $device->getVersion('WebKit');

Here's a list of all the methods on the Device class that you can utilize:

  • @todo plz list me
  • list
  • it

@ todo user apache_get_headers

Shortcuts

There are static shortcuts available for quick usage.

use MobileDetect\MobileDetect;

if (MobileDetect::isMobile() || MobileDetect::isTablet()) {
  $browser        = MobileDetect::getBrowser();
  $browserVersion = MobileDetect::getBrowserVersion();
  $os             = MobileDetect::getOperatingSystem();
  $osver          = MobileDetect::getOperatingSystemVersion();
  $model          = MobileDetect::getModel();
  $modelVersion   = MobileDetect::getModelVersion();
  $vendor         = MobileDetect::getVendor();
}

These methods aren't particularly performant, but are offered for simpler usage if the developer so desires.

Caching

As of Mobile Detect version 3, caching is supported and can be utilized with any caching library by utilizing closures that you set in your bootstraps. You need to provide a cache setter and a cache getter. Here's a simple example where you write the object to disk:

$mobileDetect = new MobileDetect();
$mobileDetect->setCacheSetter(function($key, $obj){
  $ser = serialize($obj);
  file_put_contents('/tmp/' . $key, $ser);
  return true;
});

$mobileDetect->setCacheGetter(function($key){
  $file = '/tmp/' . $key;
  
  if (file_exists($file) && is_readable($file)) {
    $raw = file_get_contents($file);
    return unserialize($raw);
  }
});

The key that is used in caching is the user agent header, which is either automatically detected using the $_SERVER suberglobal or can be injected into the constructor during initialization.

Questions

Why does my device not have a model version?

Detection is based off of known or popular patterns in user agent strings. Not all devices or user agents are known, nor do all types get reported in the user agent. We encourage you to report user agents that you find may be not fully detectable with the device class. Please open a Github issue and provide the user agent to us.

Why can I not get version by float anymore?

The version_compare method in PHP uses strings and provides any type of version comparison necessary. The ability to convert to floats is unnecessary given this method.

Why are there no more device grades?

Device grades are very expensive to detect and can add significant overhead and this type of detection is on-par with browser-specific code versus capability-detection in JavaScript.

Why are there no more quick detection methods based off of some special headers?

They don't provide the big picture and their usefulness is dependent on their popularity. However, it's unclear that these headers were ever ubiquitous enough to warrant the additional runtime necessary for inspecting them. Regardless, device information cannot be built with these headers and so their usefulness is very limted.

About

Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%