Page 1 of 1

pulling server data for my counter

Posted: Thu Sep 26, 2002 9:22 am
by waskelton4
Hey group!

I have a question for ya..

I'm building a counter for my site and I'd like to pull as much data as I can from each visit and I'd like to see how some of you do this.

I'm primarily talking about the $_SESSION['HTTP_USER_AGENT']

it's giving me strings such as:
1. Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Q31
2. Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
3. Mozilla/5.0 (X11; U; Linux i686; en-US; Nautilus/1
4. Lynx/2.8.5dev.3 libwww-FM/2.14 SSL-MM/1.4.1 OpenSS
5. Mozilla/5.0 Galeon/1.0.3 (X11; Linux i686; U;) Gec
6. Mozilla/4.77 [en] (Win95; U)
7. gnome-vfs/1.0.4

I can look at them and pretty much tell what most of them are, except i'm not quite so sure why line 7 returned so little. Anyway.. I've been using the SELECT FROM tbl WHERE data LIKE '%$type%' statement to get into the string and pull out the browser and soon the OS. My question is primarily with line #6. I know that's Netscape because i asked a friend to hit the site with Netscape, but I don't see really where it's different enough to search with WHERE LIKE.

Can anyone help me out with some ideas?

Also, does anyone have anyother things they like to pull in a counter that would be helpfull to web developers? (i.e. Screen Res, Color Depth)

Oh, and one other thing.. (last one i promise).. when i capture $_SERVER['REMOTE_ADDR'] i'm not getting the address of the machine that the browser is running on, but i think i'm getting the router sometimes.. so my IP's are per visitor. any Ideas??

Thanks Much
Will

Posted: Thu Sep 26, 2002 9:29 am
by Coco
screen res is a good one so you can decide what width to primarily make your site...

if you do any advertising, referral ip might be a handy one to take

cant think of much else right now but most th eother stuff you could take could easily be taken as a breach of privacy
(cant help with your technical questions:()

Posted: Thu Sep 26, 2002 9:38 am
by nielsene
All Netscape and Mozilla always report directly as Mozilla. You know its a "real" netscape and not some body pretending to be Netscape if the version number is not 4.0/5.0 Ie Mozilla/4.77 = Netscape 4.77

Back in the early days of the browser wars, so many people where doing hacks like shuttting out all non-netscape browsers, so all the other browser started saying they were netscape, too. That's why they all give there details as a comment and masquerade as Mozilla.

Posted: Thu Sep 26, 2002 10:04 am
by nielsene
Oh and for your IP question try using this, modified from the on-line docs at http://www.php.net/predefined.

Code: Select all

function getip() 
{
if (isSet($_SERVERї"HTTP_X_FORWARDED_FOR"]) AND
    $_SERVERї"HTTP_X_FORWARDED_FOR"]!="127.0.0.1")
{
  $realip = $_SERVERї"HTTP_X_FORWARDED_FOR"];
} 
elseif (isSet($_SERVERї"HTTP_CLIENT_IP"]))
{
  $realip = $_SERVERї"HTTP_CLIENT_IP"];
} 
else 
{
  $realip = $_SERVERї"REMOTE_ADDR"];
}
return $realip; 
}
My modifications: removed the getenv portion, OBE for most php's nowadays and added the test for some browsers that stick localhost in the X-Forwarded-For header when it wasn't really forwarded

Posted: Thu Sep 26, 2002 10:36 am
by waskelton4
Thanks for the replys!

nielsene, i tried that code, however it appears that neither $_SERVER['HTTP_X_FORWARDED_FOR'] nor $_SERVER["HTTP_CLIENT_IP"] are receiving any data. I tried to grab them directly with no conditions and post to my DB but both times the fields were blank.

Got errors..
PHP Notice: Undefined index: HTTP_CLIENT_IP
and
PHP Notice: Undefined index: HTTP_X_FORWARDED_FOR


I was hoping to be able to do this without using cookies but it appears that i might have to.

any other ideas? like how to pull screen res and color depth as well as what you might suggest doing if someone has a netscape broser with version x.0...

Thanks for you help!
Will

Posted: Thu Sep 26, 2002 11:16 am
by nielsene
Those are notices, not errors or warnings. That just means that the user's browser didn't set http_client_ip and that no proxy along the way placed the requestors IP into X-Forwarded-For while subsituting its own for remote_addr...

This means that on that test you should be able to trust remote_addr as the client's ip. Under normal conditions both x-forwarded-for and client-ip will be empty so its no good always storing them. Basically only one of the three will have the information you want.

Screen res and color depth might be possible with javascript, it won't be possible from PHP directly, nor from the sent server variables.

Here's a skeleton you can start with for your browser detection stuff, I've never really cared for doing this, so this won't be complete, but... This isn't tested at all, but I think you see the idea....

Code: Select all

$user_agent = $HTTP_SERVER_VARSї"HTTP_USER_AGENT"];
if (preg_match("|Mozilla/ї45].0|",$user_agent)
{
   // masqueraded user_agent, need to read the comments to find the real one
   if (preg_match("/.*MSIE (.*);/",$user_agent,$matches)
      $browser = "MSIE ".$matchesї0];
   if (preg_match("/.*Galeon/(.*);/",$user_agent,$matches)
      $browser = "Galeon ".$matchesї0];
   if (preg_match("/.*Nautilus/(.*);/",$user_agent,$matches)
      $browser = "Nautilus ".$matchesї0];
}
else if (preg_match("|Mozilla/(ї456].ї0-9]ї0-9])|",$user_agent,$matches)
  $browser = "Netscape $matchesї0]";
else
// other special cases

Posted: Thu Sep 26, 2002 11:33 am
by waskelton4
Ok.. i see what you mean as far as the IP's go.. but i know that the IP getting put into REMOTE_ADDR isn't correct because i'm the one testing it and my IP isn't the that's being captured..

Thanks for the Info on the notices and everything else too..

I'll check into some of that javascript and also work on that script you posted.

thanks for you help!

will