Browser Sniffer in PHP

Why should one know what the browser is ?


The broser Javascript is differnt across browsers NetScape vs IE vs Opera. So if you are sending JS for all browsers to the client then its a bad thing because


1) Your server is under more stress.
2) The code will need to resolve the user agent at the client end
3) The cleints might not visit your site again due to the slugginshness!

so this is how you do it :

global $HTTP_USER_AGENT;
$b = $HTTP_USER_AGENT; $vers = 0.0;

// detect browser brand and version number
if (eregi('Opera[ \/]([0-9\.]+)' , $b, $a)) {
$type = 'Opera';}
elseif (eregi('Netscape[[:alnum:]]*[ \/]([0-9\.]+)', $b, $a)) {
$type = 'Netscape';}
elseif (eregi('MSIE[ \/]([0-9\.]+)', $b, $a)) {
$type = 'Explorer';}


Hey smart guys , obviously you will use this code in a function or class library right . Keep it maintainable .... right ?

0 comments:

Post a Comment