SIMPLE FUNCTION TO DETECT MOBILE BROWSER WITH PHP

Hello & welcome on Tekomobile. I’m a little bit busy
nowadays so can’t update my blog regularly but don’t
worry, I will be back soon. So what have I brought today.
Nothing special dude. Just a simple function to detect if
the visitor is using mobile or not . We are gonna do it with
the help of regex. This function will return true if the
visitor is using mobile browser and false if not. You can
use it for several works. Like redirecting the visitors to
mobile version or display an element.

THE FUNCTION
Detect mobile browser with PHP

function is_it_mobile($ua)
{
$_mac_regex="/(android|samsung|^sam\-|s[cg]h|nokia|phone|pod|pad|tablet|touch|motorola|^mot\-|softbank|foma|docomo|kddi|up\.(browser|link)|";
$_mac_regex.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|ios|meego|webos|techfaith|palm|";
$_mac_regex.="blackberry|alcatel|amoi|nexian|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
$_mac_regex.="series(4|6)0|symbian|smartphone|midp|wap|windows (ce|phone)|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
$_mac_regex.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220|480x640|600x800|";
$_mac_regex.="avantgo|bolt|vodafone|pocket|pdxgw|astel|minimo|plucker|pda|xiino|cricket|silk|zune|";
$_mac_regex.="playstation|fennec|hiptop|maemo|compal|kindle|mmp|p(ixi|re)\/|psp|treo|xda|wireless| mobi";
$_mac_regex.=")/i";
if(preg_match($_mac_regex, strtolower($ua)))
{
return true;
}
return false;
}

THE EXPLANATION
Look carefully to the function. First of all we are collecting
the useragent string in the variable $ua. Then we added
few regex in $_mac_regex . They are taken from the mobile
browser’s useragent strings. Next we have checked if the
useragent matches with the regex. If it does then we
return true otherwise false .

CONCLUSION
Although this function works fine for all mobiles. But it is
not the best method to detect mobile. There are more
ways to do that. But it is useful when you don’t want to
use classes or something like that and looking for a simple
way. Drop your comments. Comments are open and
always will be

Comments

Popular posts from this blog

How to enjoy Mtn Musicplus Unlimitedly with just N15

How You Can Boost Your Opera Mini Speed.

How To Change Imei Of All Android Phones