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
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.
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>";
}
精彩评论