PHP: Detect the user’s browser type.

Posted on March 9, 2012 by Jimmy K. in Tutorials.

At some point you may need to know the user’s browser type to handle browser-specific events. Most likely, this information will be used to determine which CSS properties you can use to make a web page display the same across all browsers. This function will return the user’s browser type as a string.

/* detect the user's browser type. */
function jDetectBrowserType($sInput = "") {

if (empty($sInput)) $sInput = $_SERVER['HTTP_USER_AGENT'];
$sInput = strtoupper($sInput);

$aBrowserTypes = array("MAXTHON" => "Maxthon",
"CHROME" => "Chrome",
"SEAMONKEY" => "SeaMonkey",
"FIREFOX" => "Firefox",
"KDE" => "Konqueror",
"MSIE" => "Internet Explorer",
"NAVIGATOR" => "Netscape Navigator",
"OPERA" => "Opera",
"SAFARI" => "Safari",

foreach ($aBrowserTypes as $oKey => $oValue) {
if (strpos($sInput, $oKey) !== false) return $oValue; // we found a match..

return "";


In the function above, the less common (newer) browsers need to be in the beginning of the $aBrowserTypes array because some of them are built upon preexisting browsers. For example, the user agent for SeaMonkey returns SeaMonkey and Firefox, and the user agent for Chrome returns Chrome and Safari. If the array was ordered any other way (alphabetic, for example), then SeaMonkey users would be returned as Firefox users because SeaMonkey (S) comes after Firefox (F) in the alphabet. Interesting, huh?

An example of this function can be found here.

This function is part of a larger class I wrote called QTils. The latest version of the QTils class can be downloaded from the END[SEVEN] Cloud.

Tags: , ,


Jimmy K. is a Chicago-based web developer who actively posts tutorials, articles and insights on his web development blog to help other programmers and developers.

You can find Jimmy on Google+ and Twitter.


If you like this, please leave a comment.

Name (required)
Email Address (required)