Browser Identification Code not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Browser Identification Code not working

Post by bruceg »

Hello,

I am using PHP code to try and indentify browser and platform, but it doesn't seem to be working.

No matter what browser I use, I just get a msg stating "unidentified browser". I am sure there is a problem with the code. Here it is:

Code: Select all

<?php $viewer = getenv ( "HTTP_USER_Agent:" );

	$Browser ="an unidentified browser";
	if (preg_match( "/MSIE/i", $viewer ) )
			{$browser="Internet Explorer"; }
	else if (preg_match( "/Netscape/i", $viewer ) )
			{$browser="Netscape";  }
	else if (preg_match( "/Opera/i", $viewer ) )
			{$browser="Opera";  }
	else if (preg_match( "/Mozilla/i", $viewer ) )
			{$browser="Mozilla";  }
	else if (preg_match( "/Firefox/i", $viewer ) )
			{$browser="Firefox";  }
	else if (preg_match( "/Mozilla/i", $viewer ) )
			{$browser="Mozilla"; }
	$platform = "an unidentified operating system" ;
	if ( preg_match( "/Windows/i", $viewer ) )
		{ $platform="windows"; }
	else if(preg_match( "/Macintosh/i" , $viewer ) )
		{ $platform ="Macintosh";  }
	echo ( "<p>You're using $browser on $platform</p>" );
?>
the page can be found at http://www.inspired-evolution.com/Brows ... cation.php

Thanks in advance for any assistance.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

try:

Code: Select all

$viewer = $_SERVER['HTTP_USER_AGENT'];
I'm sure the evn var would work too, but AGENT needs to be caps.
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Post by bruceg »

thanks,

but I changed AGENT to caps and it still doesn't want to work :-(
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

did you try using the $_SERVER[] array?
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Post by bruceg »

no,

where would I put the $SERVER array in the above code?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

bruceg wrote:no,

where would I put the $SERVER array in the above code?

Code: Select all

$viewer = $_SERVER['HTTP_USER_AGENT'];
You may find it useful to print_r($_SERVER) and inspect it too ;)
R0d Longfella
Forum Newbie
Posts: 20
Joined: Fri Apr 08, 2005 7:17 am

Post by R0d Longfella »

Not all browser identify themselves. I don't know if Mozilla identifies itself by default. So first inspect $_SERVER with print_r (); You might get a clue.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Why not just dig through the code in the excellent http://phpsniff.sourceforge.net/ ?
Post Reply