Jump to content

Clean way to get browser version


mo

Recommended Posts

Hello,

 

I read through the forum for an answer but the one post that almost answered it, did not leave the solution, just a solved message.

 

I use the following to get the browser but do not have an easy to get the version number. Any help will be appreciated.

 

function determine_browser() {
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);

// Identify the browser. Check Opera and Safari first in case of spoof. Let Google Chrome be identified as Safari.
if (preg_match('/opera/', $userAgent)) {
	$name = 'OPERA';
}
elseif (preg_match('/webkit/', $userAgent)) {
  	$name = 'SAFARI';
  }
  elseif (preg_match('/msie/', $userAgent)) {
  	$name = 'IE';
  }
  elseif (preg_match('/mozilla/', $userAgent) && !preg_match('/compatible/', $userAgent)) {
  	$name = 'FIREFOX';
  }
  else {
  	$name = 'UNKNOWN';
  } 
return $name;
}

Link to comment
https://forums.phpfreaks.com/topic/165972-clean-way-to-get-browser-version/
Share on other sites

Try this:

 

<?php

/*
get_browser_

This function determines visitor browser.
*/
function get_browser_($user_agent)
{
$browsers = array(
	'Opera' => 'Opera',
	'Mozilla Firefox'=> '(Firebird)|(Firefox)',
	'Galeon' => 'Galeon',
	'Mozilla'=>'Gecko',
	'MyIE'=>'MyIE',
	'Lynx' => 'Lynx',
	'Netscape' => '(Mozilla/4\.75)|(Netscape6)|(Mozilla/4\.08)|(Mozilla/4\.5)|(Mozilla/4\.6)|(Mozilla/4\.79)',
	'Konqueror'=>'Konqueror',
	'SearchBot' => '(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp/cat)|(msnbot)|(ia_archiver)',
	'Internet Explorer 6' => '(MSIE 6\.[0-9]+)',
	'Internet Explorer 5' => '(MSIE 5\.[0-9]+)',
	'Internet Explorer 4' => '(MSIE 4\.[0-9]+)',
);

foreach($browsers as $browser=>$pattern)
{
	if (eregi($pattern, $user_agent))
		return $browser;
}
return 'Unknown';
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.