954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP Detect browser

Hello I have this code to detect the user browser, using if and else:

<?php

if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) 
		echo 'Internet explorer';

elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== FALSE) 
		echo 'Mozilla Firefox';
		
		elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== FALSE) 
			echo 'Google Chrome';
			
			else 
				echo 'Something else';
?>

Is there a way to do this using the switch statement?

I tried this, and it does not work, it just retuns default:

<?php

switch(strpos($_SERVER['HTTP_USER_AGENT'], $browser) !== FALSE) {

case $browser='MSIE':
	echo 'Internet explorer...';
		
case $browser='Firefox':
	echo 'Mozilla Firefox...';
	
case $browser='Chrome';
	echo 'Google Chrome...';	
	
default:
	echo 'Something else...';
}
?>
ovidiu_b13
Light Poster
27 posts since Sep 2010
Reputation Points: 13
Solved Threads: 1
 

Do this:

YOUR CODE

<?php

switch(strpos($_SERVER['HTTP_USER_AGENT'], $browser) !== FALSE) {

case $browser='MSIE':
	echo 'Internet explorer...';
		
case $browser='Firefox':
	echo 'Mozilla Firefox...';
	
case $browser='Chrome';
	echo 'Google Chrome...';	
	
default:
	echo 'Something else...';
}
?>


NEW CODE

<?php

switch($_SERVER['HTTP_USER_AGENT']) {
	case 'MSIE':
		echo 'Internet explorer...';
	break;
	case 'Firefox':
		echo 'Mozilla Firefox...';
	break;
	case 'Chrome';
		echo 'Google Chrome...';	
	break;
	default:
		echo 'Something else...';
}
?>
P0lT10n
Posting Whiz in Training
235 posts since Apr 2010
Reputation Points: 13
Solved Threads: 30
 

It doesn't work. This goes to default to.

ovidiu_b13
Light Poster
27 posts since Sep 2010
Reputation Points: 13
Solved Threads: 1
 

Hello $_SERVER['HTTP_USER_AGENT'] don’t bring just “Firefox” but something like “Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3” depending on the system – browser user use. Instead of making your own parsing method PHP has the get_browser function witch returns an array of informations (if available) of visitors browser. Use the “browser” key and you will get “Firefox”.

Take a look at
http://php.net/manual/en/function.get-browser.php

(And a personal coding suggestion, use , if , else if , else instead of switch statement , maybe it is more verbal but made your code a lot cleaner, in my point of view… )

jkon
Posting Whiz in Training
281 posts since Jan 2009
Reputation Points: 77
Solved Threads: 47
 

I agree with jkon, the only caveat is needing a browscap.ini which isn't packaged with php by default. This can only be set in the php.ini or httpd.conf files.

mschroeder
Work Harder
Team Colleague
666 posts since Jul 2008
Reputation Points: 279
Solved Threads: 131
 

I want to use switch because 1. to practice coding, 2. Because Switch is faster than if else. I don't understand the get_browser() yet. Still studying. I've studied from w3schools.com, but the tutorial was not good enough, so now I'm going to the main source: php.net (manual). Went over the

if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) 
		echo 'Internet explorer';

and wanted to practice a little.

ovidiu_b13
Light Poster
27 posts since Sep 2010
Reputation Points: 13
Solved Threads: 1
 

Switch is not faster than if else … (I can give code about it)… maybe it could be in 1000 switches for 0.01 second … but … this is not programming …

jkon
Posting Whiz in Training
281 posts since Jan 2009
Reputation Points: 77
Solved Threads: 47
 

This is what I've learned from my C++ teacher, and I thought this applies to every language. I see that you are a php wizz, do you think learning from php.net manual will get me anywhere? My current level is Beginner.

ovidiu_b13
Light Poster
27 posts since Sep 2010
Reputation Points: 13
Solved Threads: 1
 
This is what I've learned from my C++ teacher, and I thought this applies to every language. I see that you are a php wizz, do you think learning from php.net manual will get me anywhere? My current level is Beginner.


Yes !

P0lT10n
Posting Whiz in Training
235 posts since Apr 2010
Reputation Points: 13
Solved Threads: 30
 

Since you mentioned that I think that you should now a lot of deferent languages and master some of them, Cobol , basic , java , php. And not only know them, work with them as an employ first , that is how you learn the job … even your degrees … The basic think for me (maybe I am wrong) is the algorithm and object oriented way of thinking …

jkon
Posting Whiz in Training
281 posts since Jan 2009
Reputation Points: 77
Solved Threads: 47
 

Unfortunately I am learning on my own. The C++ teacher I mentioned was only temporary (3-4 months), but that helped me get started. I got hired by my dean to make the school website, and that is how I've learned HTML, CSS and Joomla. Now I want to get more advanced. By the time I finish this so called College, I want to be able to get hired as a Web Developer (Not WebDesign!). I want to make web applications not websites.

I know a bit of C++, C#, Java yet still I don't exactly understand the notion of objects.

ovidiu_b13
Light Poster
27 posts since Sep 2010
Reputation Points: 13
Solved Threads: 1
 

Master Java and you will know ... learn a framework then like JSF and learn PHP ... don't think PHP as an inferior language but as superior giving you the opportunity to write OOP and think as you like ... Coding strict .. Choose your editor (eclipse) ... I said much ... that is my opinion ... programming best techniques is not an algorithm but has to do with peoples involving it. ...

jkon
Posting Whiz in Training
281 posts since Jan 2009
Reputation Points: 77
Solved Threads: 47
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: