Friday, April 5, 2013

PHP Class to Detect Mobile Device

Hello friends,

Today in Developer's Blog I will tell you the way to Detect Mobile Device using PHP Class.

So this Class will help you to redirect to Mobile Website (If you develop that also along with web version) and will also help you redirect to Web version of the website if someone tries to open Mobile Version on the Desktop PC.

So below is the PHP Class to Detect Mobile Device.


<?php
class Mobile_Detect {

protected $accept;

protected $userAgent;
protected $isMobile     = false;
protected $isAndroid    = null;
protected $isBlackberry = null;
protected $isOpera      = null;
protected $isPalm       = null;
protected $isWindows    = null;
protected $isGeneric    = null;

protected $devices = array(
    "android"       => "android",
    "blackberry"    => "blackberry",
    "iphone"        => "(iphone|ipod)",
    "opera"         => "opera mini",
    "palm"          => "(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)",
    "windows"       => "windows ce; (iemobile|ppc|smartphone)",
    "generic"       => "(kindle|mobile|mmp|midp|o2|pda|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap)"
);


public function __construct() {
    $this->userAgent = $_SERVER['HTTP_USER_AGENT'];
    $this->accept    = $_SERVER['HTTP_ACCEPT'];

    if (isset($_SERVER['HTTP_X_WAP_PROFILE'])|| isset($_SERVER['HTTP_PROFILE'])) {
        $this->isMobile = true;
    } elseif (strpos($this->accept,'text/vnd.wap.wml') > 0 || strpos($this->accept,'application/vnd.wap.xhtml+xml') > 0) {
        $this->isMobile = true;
    } else {
        foreach ($this->devices as $device => $regexp) {
            if ($this->isDevice($device)) {
                $this->isMobile = true;
            }
        }
    }
}


/**
 * Overloads isAndroid() | isBlackberry() | isOpera() | isPalm() | isWindows() | isGeneric() through isDevice()
 *
 * @param string $name
 * @param array $arguments
 * @return bool
 */

public function __call($name, $arguments) {
    $device = substr($name, 2);
    if ($name == "is" . ucfirst($device)) {
        return $this->isDevice($device);
    } else {
        trigger_error("Method $name not defined", E_USER_ERROR);
    }
}


/**
 * Returns true if any type of mobile device detected, including special ones
 * @return bool
 */
public function isMobile() {
    return $this->isMobile;
}


protected function isDevice($device) {
    $var    = "is" . ucfirst($device);
    $return = $this->$var === null ? (bool) preg_match("/" . $this->devices[$device] . "/i", $this->userAgent) : $this->$var;

    if ($device != 'generic' && $return == true) {
        $this->isGeneric = false;
    }

    return $return;
}
}
?>

And then write below line of code on the Web version in the file which you are including on every page to redirect to mobile site from all pages.

<?php

include('Mobile_Detect.php');
$detect = new Mobile_Detect();
if ($detect->isMobile()) 
{
    // it is mobile platform
    header("Location:http://www.mobile.mywebsite.com");
}


?>

And if you want to redirect user who tries to access mobile version from Desktop PC then write below code in the file which you are including on every page to redirect to web version.

<?php



include('Mobile_Detect.php');
$detect = new Mobile_Detect();
if (!$detect->isMobile()) 
{
    // it is desktop pc
    header("Location:http://www.mywebsite.com");
}

?>

Download Source Files

So by this way, you have successfully Detected Mobile Device from a PHP Class.

I hope this will help you and you will like it.

Don't forget to leave your comments.


Thank you
Ravinder


1 comment: