Categories
PHP

PHP : How to check Browser on Server Side using HTTP_USER_AGENT

Being a PHP Programmer sometime you need to check the browser and browser version to resolve some coding bug or change the functionality as per the browser. Internet Explorer is always a headache for me and this is why sometimes i need to check the Internet Explorer Version while coding. So today i am sharing a very simple PHP code to check the browser on Server Side using HTTP_USER_AGENT. Hope this will save your soul in need.

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.

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 Reply

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