Log visitors to file
Do you know how many visitors you get to your site every day, and where they come from? Why not log it your self with this pretty simple technique and save the statistics to a plain file of the web server.
There are many different ways to save statistics. And many of them save TO much information. I will start you up with a solution that save only unique visitors, where they come from, when and what page they looked at.
Q: So how can we know if the visitor is new to the site?
A: Start a session for all visitors. All visitors without a session are new
Enough talking alright… Here comes the PHP code:
<?php
session_start();
if($_SESSION["logged"] != "yes")
{
$agent = $_SERVER['HTTP_USER_AGENT'];
$uri = $_SERVER['REQUEST_URI'];
$ip = $_SERVER['REMOTE_ADDR'];
$ref = $_SERVER['HTTP_REFERER'];
$visitTime = date("r"); //Example: Thu, 21 Dec 2000 16:01:07 +0200
$logLine = "$visitTime - IP: $ip || User Agent: $agent || Page: $uri || Referrer: $ref\n";
$fp = fopen("visitorLog.txt", "a");
fputs($fp, $logLine);
fclose($fp);
$_SESSION["logged"] = "yes";
}
?>
The code is pretty streight forward. But some explanation might be handy.
First we start a session. Check if the session variable “logged” is NOT “yes”. In that case we have a new visitor. Use the PHP variable $_SESSION and get all interesting data about the request and the user. I choose “Useragent”, “uri” (what page is accessed), ip (Visitors IP address), ref (What page did the user find your site through?)
After that we build the row we want to save to our log file.
fopen() opens a file in append mode (creates if not there, otherwise append to existing at the bottom)
Make sure the directory/file is writable for the web server. SECURITY: it might be smart to put the log file out of the web root, so you don’t need to set write access in your root and that people can see your stats.
fputs() writes the row in the file and fclose… closes the file
Surprise….
Last we set the session variable logged to “yes” so we know that we don’t need to log the visitor next page he looks at.
Next week I will create a tutorial about how to add similiar information to a database and show nice stats… so stay tuned!

May 3rd, 2009 at 3:14 pm
just a little mistake…
[..]
$logLine = ā$visitTime – IP: $ip || User Agent: $agent || Page: $uri || Referrer: $ref\nā;
[..]
fputs($fp, $entry_line);
[..]
variable $entry_line should be $logLine
May 3rd, 2009 at 3:30 pm
Thanks Enrice, it has been corrected now!
September 4th, 2009 at 5:34 am
Spot on, just what I was looking for, thanks very much!
September 4th, 2009 at 6:20 am
Glad to be able to help! Always feel free to link our site up if you like to
September 10th, 2009 at 3:09 am
I happily will, when I’ve got round to implementing it, I haven’t had any chance to implement it yet
February 15th, 2010 at 11:43 am
what about race conditions?
In reality, your site might get 2 or 5 or many requests at (approximately) the same time… btw, how many threads does your apache run? correct – many… so that means that on line 12 there will start competition about who’s gonna write to file
March 5th, 2010 at 2:35 am
xvga: You are absolutely right! I’ll see if we can investigate a little on the best way to prevent race conditions for an upcoming tutorial.
One simple way that I know some people use is to create a random name for the file and save a one copy for every row you write, and take care of merging them in another process.