Php Code Detect Mobile Browser - Php
How to detect mobile browser using php code?
Snippet Code
Here "checkBrowser()" function is used to detect mobile browser in PHP.
<?php
function checkBrowser() {
if (preg_match("/Mobile|Android|BlackBerry|iPhone|Windows Phone/", $_SERVER['HTTP_USER_AGENT'])) {
$useragent = $_SERVER['HTTP_USER_AGENT'];
$browser = "Unknown Browser";
if (preg_match('/msie/i', $useragent) amp&!preg_match('/opera/i', $useragent)) {
$browser = "Internet Explorer";
} else if (preg_match('/firefox/i', $useragent)) {
$browser = "Firefox";
} else if (preg_match('/chrome/i', $useragent)) {
$browser = "Chrome";
} else if (preg_match('/safari/i', $useragent)) {
$browser = "Safari";
} else if (preg_match('/opera/i', $useragent)) {
$browser = "Opera";
} else if (preg_match('/netscape/i', $useragent)) {
$browser = "Netscape";
}
return $browser;
}
}
echo checkBrowser();
if(checkBrowser())
{
echo "Mobile device detected";
}
else
{
echo "Desktop detected";
}
?>
Tags