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';
}
?>

By Vaseem Ansari

Iā€™m Vaseem, a Software Engineer by Profession, a Traveler, a Foodie by Heart and the founder of VaseemAnsari.com. I started traveling from college days and it has become a part of me now. So when ever I happen to get a chance I pack my bag and am on the roads. You can follow me on these social networks Facebook, Twitter, Google+ and Linkedin.

Leave a comment

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