4 Replies - 2384 Views - Last Post: 27 December 2002 - 12:08 PM Rate Topic: -----

#1 Garstor  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 09-October 02

Identifying browsers in PHP

Posted 27 December 2002 - 10:25 AM

Does anybody know of a standard method of identifying the visiting browser using PHP? I know about the $HTTP_SERVER_VARS code (for some reason the newer $_SERVER superglobal doesn't work, though I'm pretty sure my provider has the latest PHP version).

So basically, we examine the string returned by $HTTP_SERVER_VARS['HTTP_USER_AGENT']. However, I want to avoid any possible gotchas; for example, IE inserts the word "Mozilla" so testing for that string does not guarantee identifying a true Mozilla browser. Opera can identify itself as a different browser; is there any way around that?

In short, I'm in no mood to download every single browser and version out there just to examine one blasted string variable. Any assistance would be welcome!

Thanks,

Is This A Good Question/Topic? 0
  • +

Replies To: Identifying browsers in PHP

#2 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Identifying browsers in PHP

Posted 27 December 2002 - 11:01 AM

wouldn't you use javascript for that?
Was This Post Helpful? 0
  • +
  • -

#3 Garstor  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 09-October 02

Re: Identifying browsers in PHP

Posted 27 December 2002 - 11:20 AM

fyrestorm, on Dec 27 2002, 12:01 PM, said:

wouldn't you use javascript for that?

Well you certainly code use Javascript for that ... I prefer to put the code server-side (considering my site is just for personal hobbying and not running a true web-application I doubt it will tax my ISP's servers).

Here is the code as I currently have it:
$useragent = $HTTP_SERVER_VARS['HTTP_USER_AGENT'];
echo "USER_AGENT = $useragent<BR><BR>";

if (preg_match ("/msie/i", $useragent)) {
    print "You are using Internet Exploiter.";
} elseif (preg_match ("/opera/i", $useragent)) {
    print "Good for you! You are using Opera!";
} elseif (preg_match ("/mozilla/i", $useragent)) {
    print "Are you using Netscrape or Mozilla?";
}



I test for Internet Exploiter first since its the most popular. Instead of using print to spit something to the screen I'll just set a variable that can be checked later in the file.

I leave Mozilla for the last because of the issue I mentioned with that string being in IE's USER_AGENT.

Right now, my only use for this information is in my CSS. I started a thread in the HTML/Javascript forum about simulating tables in CSS -- that mutated into an issue I've had with Mozilla not reading my CSS. I discovered that it will work ONLY if I define it inline instead of linking to an external file.

I'm planning to read the file and output it inline if the visiting browser is Mozilla.

Are there better ways to handle this? What about "session"? I've heard about that but don't know how to use it.

Regards,
Was This Post Helpful? 0
  • +
  • -

#4 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4425
  • View blog
  • Posts: 28,355
  • Joined: 21-March 01

Re: Identifying browsers in PHP

Posted 27 December 2002 - 11:33 AM

http://www.php.net/m...get-browser.php

that should be what your lookin for as far as php goes. i guess once we get this problem working we can start on your others. :)
Was This Post Helpful? 0
  • +
  • -

#5 Garstor  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 09-October 02

Re: Identifying browsers in PHP

Posted 27 December 2002 - 12:08 PM

supersloth, on Dec 27 2002, 12:33 PM, said:

http://www.php.net/m...get-browser.php

that should be what your lookin for as far as php goes. i guess once we get this problem working we can start on your others. :)

Yes, I was looking at that function. It didn't seem to work properly with my ISP (much like this issue with Mozilla).

As for my other problems...well, they are many and varied (or are we limiting them to just "coding problems"? :) )

Here's the scoop...I just got Mozilla to properly interpret my CSS by file reading it inline. But IE and Opera have now broken -- displaying the page's output EXACTLY the same way Mozilla did when it wasn't working...

AAAAAAAAAAAGGGGGHHHH!!! Thank-you, I needed that. :crazy:

Here's the PHP code I used:
	if (preg_match ("/msie/i", $useragent)) {
  $browser = "IE";
	} elseif (preg_match ("/opera/i", $useragent)) {
  $browser = "Opera";
	} elseif (preg_match ("/mozilla/i", $useragent)) {
  $browser = "Mozilla";
	}
	
	if ($browser == "Mozilla") {
  echo "<style type=\"text/css\" media=\"all\">";
	
  $fd = fopen ("./3Col.css", "r");
  while (!feof ($fd)) {
 	 $buffer = fgets($fd, 4096);
 	 echo $buffer;
  }
  fclose ($fd);
  
  echo "</style>";
	} else {
  echo "<style type=\"text/css\" media=\"all\">";
  echo "@import \"3Col.css\"";
  echo "</style>";
	}


I looked at both IE's and Opera's source output and, as expected, it contained the line "<style type="text/css" media="all">@import "3Col.css"</script>". This is the same string I had used previously when both IE and Opera was properly displaying the CSS rules...

OK...I fixed it up before posting this message... When using the @import method of linking, a semi-colon must appear. So it really should be "<style type="text/css" media="all">@import "3Col.css";</script>"

Hey, sloth, was that one of my other problems? ;)
Regards,
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1