• 14th, Jun 2010

PHP Browser Functions

/**
* Browser Detection Functions
* www.unexpectedit.com
*/
function inAgent($agent) {
	global $_SERVER;
	return (strpos($_SERVER['HTTP_USER_AGENT'], $agent) === false) ? false : true;
}

function whichBrowser() {
	/*
	* $browser will contain one of the following values:
	* 'iewin' : IE 4+ for Windows
	* 'iemac' : IE 4 for Macintosh
	* 'ie5mac' : IE 5 Macintosh
	* 'nswin' : Netscape 4.x Windows
	* 'nsunix' : Netscape 4.x Unix
	* 'nsmac' : Netscape 4.x Mac
	* 'ns6' : Netscape 6 / Mozilla
	*/
	switch (inAgent('MSIE'))
	{
		case true:
			if ( inAgent('Mac') ) {
				$browser = inAgent('MSIE 5') ? 'ie5mac' : 'ie4mac';
			}
			elseif ( inAgent('Win') )
			{
				$browser = 'iewin';
			}
			break;

		case false:
			if (inAgent('Mozilla/5')) {
				$browser = 'ns6';
			}
			elseif (inAgent('Mozilla/4'))
			{
				if ( inAgent('Mac'))
				{
					$browser = 'nsmac';
				}
				else
				{
				$browser = (inAgent('Win')) ? 'nswin' : 'nsunix';
				}
			}
			else
			{
				$browser = "unknown";
			}
			break;
	}
	return($browser);
}

// browser detection - end
switch (whichBrowser())
{
	case 'iewin': case 'iemac': case 'ie5mac':
		echo "independent";
		break;
	case 'nswin': case 'nsunix': case 'nsmac': case 'ns6':
		echo "microsoft";
		break;
}

Tags: , , ,

Leave a Reply

*

© 2010 unexpected[it]. All Rights Reserved.