We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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...';
}
?>
5
Contributors
11
Replies
19 Hours
Discussion Span
1 Year Ago
Last Updated
17
Views
ovidiu_b13
Light Poster
27 posts since Sep 2010
Reputation Points: 13
Solved Threads: 1
Skill Endorsements: 0

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
236 posts since Apr 2010
Reputation Points: 13
Solved Threads: 30
Skill Endorsements: 0

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

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

Hello $_SERVER 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
382 posts since Jan 2009
Reputation Points: 124
Solved Threads: 63
Skill Endorsements: 4

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
673 posts since Jul 2008
Reputation Points: 281
Solved Threads: 133
Skill Endorsements: 5

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
Skill Endorsements: 0

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
382 posts since Jan 2009
Reputation Points: 124
Solved Threads: 63
Skill Endorsements: 4

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
Skill Endorsements: 0

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
236 posts since Apr 2010
Reputation Points: 13
Solved Threads: 30
Skill Endorsements: 0

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
382 posts since Jan 2009
Reputation Points: 124
Solved Threads: 63
Skill Endorsements: 4

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
Skill Endorsements: 0

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
382 posts since Jan 2009
Reputation Points: 124
Solved Threads: 63
Skill Endorsements: 4

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0893 seconds using 2.69MB