1,105,534 Community Members

PHP Detect browser

Member Avatar
ovidiu_b13
Light Poster
27 posts since Sep 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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...';
}
?>
Member Avatar
P0lT10n
Posting Whiz in Training
237 posts since Apr 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 30 [?]
Skill Endorsements: 0 [?]
 
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...';
}
?>
Member Avatar
ovidiu_b13
Light Poster
27 posts since Sep 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
jkon
Posting Pro in Training
443 posts since Jan 2009
Reputation Points: 110 [?]
Q&As Helped to Solve: 74 [?]
Skill Endorsements: 6 [?]
 
1
 

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… )

Member Avatar
mschroeder
Work Harder
655 posts since Jul 2008
Reputation Points: 251 [?]
Q&As Helped to Solve: 134 [?]
Skill Endorsements: 8 [?]
Team Colleague
 
0
 

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.

Member Avatar
ovidiu_b13
Light Poster
27 posts since Sep 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
jkon
Posting Pro in Training
443 posts since Jan 2009
Reputation Points: 110 [?]
Q&As Helped to Solve: 74 [?]
Skill Endorsements: 6 [?]
 
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 …

Member Avatar
ovidiu_b13
Light Poster
27 posts since Sep 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
P0lT10n
Posting Whiz in Training
237 posts since Apr 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 30 [?]
Skill Endorsements: 0 [?]
 
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 !

Member Avatar
jkon
Posting Pro in Training
443 posts since Jan 2009
Reputation Points: 110 [?]
Q&As Helped to Solve: 74 [?]
Skill Endorsements: 6 [?]
 
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 …

Member Avatar
ovidiu_b13
Light Poster
27 posts since Sep 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
jkon
Posting Pro in Training
443 posts since Jan 2009
Reputation Points: 110 [?]
Q&As Helped to Solve: 74 [?]
Skill Endorsements: 6 [?]
 
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. ...

Member Avatar
Tom_6
Newbie Poster
1 post since Sep 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

A friend using google chrome is being told I am using Safari, heres my code:

$user_agent = $_SERVER['HTTP_USER_AGENT']; 

if (preg_match('/MSIE/i', $user_agent)) { $browser = "Internet Explorer";} 
elseif (preg_match('/Firefox/i', $user_agent)){$browser = "Mozilla Firefox";} 
elseif (preg_match('/Chrome/i', $user_agent)){$browser = "Google Chrome";} 
elseif (preg_match('/Safari/i', $user_agent)){$browser = "Safari";} 
elseif (preg_match('/Opera/i', $user_agent)){$browser = "Opera";}
else {$browser = "Other";}
Member Avatar
diafol
Where are my eyes?
12,977 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,848 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

Chrome gives this on my machine:

Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36

And outputs...

Google Chrome

Create a dummy page like below and send him/her the url and ask them to tell you what they get.

$user_agent = $_SERVER['HTTP_USER_AGENT']; 
echo $user_agent;
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: