You may have tried using $_SERVER['HTTP_USER_AGENT'] couple of times to get the name of your website visitor's browser. Unfortunately all browsers identify themselves with a long string that all started with Mozilla, for instance when you use the above PHP function Google Chrome will return this Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36.
However, in that string, all browsers contain a distinct name. So you can use that name to identify the users browser. You can use the stripos() PHP function to find the name in the string that is returned by the browser. The code below did that for you, you can just copy and paste it to see how it works, but i recommend you understand the logic, by using echo $_SERVER['HTTP_USER_AGENT']; in many browsers to see how each browser display the string.
<!DOCTYPE html>
<head>
<title>Testing</title>
</head>
<body>
$browser = $_SERVER['HTTP_USER_AGENT'];
if (stripos($browser, 'opr') == true)
{
echo "Your Browser is Opera.";
}
else if (stripos($browser,'chrome') == true)
{
echo "Your Browser is Google Chrome.";
}
else if (stripos($browser,'trident') == true)
{
echo "Your Browser is Internet Explorer.";
}
else if (stripos($browser,'flock') == true)
{
echo "Your Browser is flock.";
}
else if (stripos($browser,'firefox') == true)
{
echo "Your Browser is Mozilla Firefox.";
}
else if (stripos($browser,'safari') == true)
{
echo "Your Browser is Safari.";
}
?>
</body>
</html>
No comments:
Post a Comment