homepage Welcome to WebmasterWorld Guest from 54.166.100.8
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Getting PHP session variables into your Apache logs
tracking users after login
jatar_k




msg:1294370
 9:47 pm on Jan 7, 2004 (gmt 0)

I came upon the problem of increasing user tracking on our secure server. I wanted to attach the user's account id to the log line to help me track what people do on the system. I often have to analyze different behaviours that trip certain flags within the system.

Apache logs are a primary source along with various syslogs and, of course, many different log parsers.

Not knowing quite what we could, and couldn't, do we started messing with various super global arrays. Our primary though was to use the $_ENV array but it would seem that Apache doesn't have access to it in the way you would think.

So we spent some time messing around with various custom log formats [httpd.apache.org] looking for something we could use. We were looking at these two possibilities.

%...{FOOBAR}e: The contents of the environment variable FOOBAR
%...{Foobar}n: The contents of note "Foobar" from another module.

There are always portions of php.net that you don't use often and therefore we forget that they are there. So we spent a lot of time on the apache.org site and then finally ended up looking at the PHP Apache specific functions [ca.php.net].

With two of us working simultaneously on this (programmer and sysadmin) we ended up trying apache_note [ca.php.net] first, though I imagine that apache_setenv [ca.php.net] would also work with the e instead of n in the LogFormat line.

We used this log format in the httpd.conf

LogFormat "%h %l %u %t \"%r\" %>s %b Referer:\"%{Referer}i\" \"%{User-Agent}i\" UserID:\"%{accountid}n\" ContactID:\"%{contactid}n\"" userid

and used this

apache_note('accountid',$_SESSION['accountid']);
apache_note('contactid',$_SESSION['contact_id']);

in our header file.

We also sorted out all of the image requests into another log so the true log becomes much more concise.

SetEnvIf Request_URI \.gif$ image
SetEnvIf Request_URI \.jpg$ image
SetEnvIf Request_URI \.png$ image
SetEnvIf Request_URI \.ico$ image

CustomLog "/site/log/site_img.log" common env=image
CustomLog "/site/log/site_accountid" userid env=!image

We have more plans for making our logging more useful but this is what we have started with. I love reading logs but most of the data in there I only refer to once in a while. It will help keep our primary logs down in size and help the many custom parsers become more efficient.

 

jatar_k




msg:1294371
 11:49 pm on Jan 8, 2004 (gmt 0)

shameless mod bump

ergophobe




msg:1294372
 12:38 am on Jan 9, 2004 (gmt 0)


shameless mod bump

We'll all hold it against you in the future ;-)

Not that it isn't interesting and impressive...

I have thought of using a database to accomplish similar things. Your method obviously has way less overhead, it's just that it's sort of over my head!

Cheers,

Tom

jatar_k




msg:1294373
 2:09 am on Jan 9, 2004 (gmt 0)

shameless mod bump

I posted yesterday and left it hidden so I couldn't figure out how else to get it up where someone might see it once I made it viewable ;)

It ended up being fairly straight forward actually. It just doesn't work too well for public sites where there is no user auth or session data to use for tracking.

It has already proved monstrously helpful.

mipapage




msg:1294374
 5:19 pm on Jan 9, 2004 (gmt 0)

Wow jatar_k,

how timely! I've been wondering if something like this could be done for an upcoming project that we have.

Thanks a lot for sharing this info.

I have thought of using a database to accomplish similar things...less overhead

I have something doing just this right now, and you are right, heavy traffic = tonnnes of data.

DrDoc




msg:1294375
 5:22 pm on Jan 9, 2004 (gmt 0)

Neato, jatar_k! This will come in handy :)

<copying style="shamelessly" /> ;)

lorax




msg:1294376
 5:39 am on Jan 10, 2004 (gmt 0)

It ended up being fairly straight forward actually. It just doesn't work too well for public sites where there is no user auth or session data to use for tracking.

What if you required a user to login on a public website? Would you be able to tap the apache server for that info?

coopster




msg:1294377
 2:42 pm on Jan 10, 2004 (gmt 0)

Slick, jatar_k, slick. Thanks for sharing :)

jatar_k




msg:1294378
 7:40 pm on Jan 10, 2004 (gmt 0)

user to login on a public website

definitely. Essentially you can add anything to the log you want with this method. If you are tracking a user in any way you can do this. You could use cookie data, session data or anything else you want.

lorax




msg:1294379
 8:09 pm on Jan 10, 2004 (gmt 0)

Slick - dangerous for those of us willing to experiment... ;)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved