Detecting user browser with PHP and displaying notice accordingly
What is the most efficient way to detect the user's browser using PHP? I need a function to block users with IE from accessing my website pages and pop them a link towards Chrome, Ff and Safari (they will choose which one they wish to download...). (Reason: I'm using various jqu开发者_开发百科ery functions and some scripts that don't work/show up properly on IE and I want my visitors to have the optimal experience).
Perhaps, $_SERVER['HTTP_USER_AGENT']
can help
I wrote this code to add css according to the browsers as the same css wont work for ff and chrome...This might help you.
<?php
$b = $_SERVER['HTTP_USER_AGENT'];
if(strstr(strtolower($b),"msie"))
echo '<link rel="stylesheet" type="text/css" href="ie.css">';
else if(strstr(strtolower($b),"firefox"))
echo '<link rel="stylesheet" type="text/css" href="ff.css">';
else if(strstr(strtolower($b),"chrome"))
echo '<link rel="stylesheet" type="text/css" href="cr.css">';
?>
Happy coding...
On the server side, all you can do is look at the HTTP User-Agent header. Have a look at the get_browser
function, which parses this for you. This is voluntary information though and not guaranteed to be accurate.
You can use get_browser
function and browscap.ini file that is maintained by Browser Capabilities Project.
精彩评论