Detect iPad and iPhone with PHP

Image for Detect iPad and iPhone with PHP from unsplash.com

By Andrew McCombe
July 25, 2012


If you ever have the need to detect iPad and iPhone with PHP, or any other mobile/tablet device, you can use the excellent php-mobile-detect library available at http://code.google.com/p/php-mobile-detect/.

Example usage:

<?php
require_once "Mobile_Detect.php";
$detect = new Mobile_Detect();

if  ($device->isMobile()) {
    // code for mobile devices
}

if ($device->isTablet()) {
    // code for tablet devices
}

if ($detect->isiOS()) {
    // code for iOS devices.
}

if ($detect->isAndroidOS()) {
    // code for Android devices.
}

If you want to detect mobile devices but not tablets you can use the following code snippet:

<?php
function is_mobile() {
    require_once 'Mobile_Detect.php';
    $device = new Mobile_Detect();

    if  ($device->isMobile()) {
        return ($device->isTablet() ? FALSE : TRUE);
    }
    return FALSE;
}