Welcome to WebmasterWorld Guest from 54.166.46.226

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Getting PHP session variables into your Apache logs

tracking users after login

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

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

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

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



shameless mod bump
12:38 am on Jan 9, 2004 (gmt 0)

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month




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

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

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

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

WebmasterWorld Senior Member 10+ Year Member



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.

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Neato, jatar_k! This will come in handy :)

<copying style="shamelessly" /> ;)

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

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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?

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

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Slick, jatar_k, slick. Thanks for sharing :)
7:40 pm on Jan 10, 2004 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

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

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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