How-to-check-Browser-Version-on-Server-Side-using-HTTP_USER_AGENT

PHP : How to check Browser on Server Side using HTTP_USER_AGENT

While dealing with jQuery and JavaScript i sometime need to check the browser version and then code accordingly. The following simple php code will let you know the correct browser version :)

<?php	//	Added by Vaseem to check the IE Browser Version on Server Side using HTTP_USER_AGENT
if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE){
	echo 'Internet explorer';
	if (stristr($_SERVER['HTTP_USER_AGENT'], "msie 10")){	//	Use msie 8, msie 9 to check if browser is IE8 or IE9
		echo 'IE 10';
	}
}elseif (preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']) || (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false)) {
	echo 'IE 11';
}elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== FALSE){
	echo 'Mozilla Firefox';
}elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== FALSE){
	echo 'Google Chrome'; 
}elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== FALSE){
	echo "Opera Mini"; 
}elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== FALSE){
	echo "Opera"; 
}elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== FALSE){
	echo "Safari"; 
}else{
	echo 'Something else';
}
?>

Stay in touch

For a daily email update, click HERE or enter your email and stay on top of things by receiving weekly news in your inbox.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>