Browser detection

Detecting browsers is a simple yet fun trick to show off on your site. Plus, it’ll impress those n00b friends of yours 😉

For this script, we will be using stristr. stristr is much easier to use due to the fact it is not case sensitive. Comments in the script are included, so it is very self explantory from this point out.

<?php

$uagent = $_SERVER['HTTP_USER_AGENT'];

/*
This gets the Content of the User-Agent Header
EG:
Mozilla/5.0 (compatible; Konqueror/3.3; Linux 2.6.8-gentoo-r3; X11;
Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.2 (KHTML, like Gecko) Safari/125.8
*/

if(stristr($uagent, "firefox")) {
// if firefox is found in the User-String
echo("Mozilla Firefox");
// Print Firefox
}
elseif(stristr($uagent, "opera") OR stristr($uagent, "msie") && stristr($uagent, "opera")) {
/* Since Opera 8 string contains both msie and opera we tell php that if  opera is found OR both opera and msie are found print opera */
echo("Opera");
}
elseif(stristr($uagent, "msie")) {
// If msie Is found in the String
echo("Internet Explorer");
// Print Internet Explorer
}
?>

You can continue this method for all other browsers, simply by replacing the word to look for and what to echo out.

Advertisements

0 Responses to “Browser detection”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: