Friday, August 30, 2013

PHP, to detect mobile device



The following PHP function is used to detect mobile device: ipod, iphone, blackberry and android
, if from mobile device, the function will return true.
function is_mobile_device() {
    $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    return ((stripos($agent, 'ipod') !== false && stripos($agent, 'ipod') >= 0) ||
            (stripos($agent, 'iphone') !== false && stripos($agent, 'iphone') >= 0) ||
            (stripos($agent, 'blackberry') !== false && stripos($agent, 'blackberry') >= 0) ||
            (stripos($agent, 'android') !== false && stripos($agent, 'android') >= 0))
            ? true : false;
}


Note: _SERVER['HTTP_USER_AGENT'] is used to retrieve information about the users browser, computer operating system and mobile type.
striposFind the position of the first occurrence of a case-insensitive substring in a string.
Returns FALSE if the needle was not found.

No comments:

Post a Comment