pravin
pravin

Reputation: 2155

Detect on which browser current web application is running

I wanted to know in PHP, how to detect on which browser my web application is running.

e.g.

If current browser is chrome then alert("Chrome browser processing") otherwise alert("rest browser processing");

echo $_SERVER['HTTP_USER_AGENT'];

Below output I am getting If i execute above code :

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.195.21 Safari/532.0

Please suggest your pointers to detect exact browser name.

Thanks,

-Pravin

Upvotes: 2

Views: 856

Answers (2)

user505255
user505255

Reputation: 320

This has some useful code.

Basically, in your case, you can just look for the string "Chrome". In general, it might take a bit more logic, since, for example, the string "Safari" is found in the user-agents provided by other browsers than Safari (including Chrome). PHP provides the 'browser' element for this.

Upvotes: 2

esqew
esqew

Reputation: 44701

Personally, I'd use Javascript for this one.

if(navigator.userAgent.match(/Chrome/i)) {
    alert("You're using Chrome!");
}
else {
    alert("You're using something other than Chrome!");
}

... but if you really wanted to, you could accomplish the same thing in PHP:

if (preg_match("/Chrome/i", $_SERVER['HTTP_USER_AGENT']) == 0) {
    // zero matches
    echo "<script>alert('You're not using Chrome!')</script>";
} else {
    echo "<script>alert('You're using Chrome!')</script>";
}

Upvotes: 1

Related Questions