I'm rather new to php, and here's the situation:
I wrote this script that opens a session, logs the ip/hostname
of the user, and writes the info on a new line in a text file. The session sets a variable so that only 'new' page requests are written.
The script works fine, but what I am confounded by is trying to implement a counter that writes the number of unique page requests at the top of the same text file.
allow me to further illustrate by showing an example of the text file:
NUMBER OF HITS:
10.112.32.103 [10.112.32.103] visited /test_stuff/fopen_test.php on: 12-02-2003 at 13:44:08
10.112.32.103 [10.112.32.103] visited /test_stuff/fopen_test.php on: 12-02-2003 at 13:44:08
What I'm trying to do is write the number of total unique hits right after where it says "NUMBER OF HITS" in the textfile. I've tried dabbling in implode(), fseek(), ad nauseum, but this has me stumped...
Anyone?
Here's the script:
<?php
session_start();
$filename = "count_hits.txt";
if(!getenv("REMOTE_HOST")) {
$_SESSION['user'] = getenv("REMOTE_ADDR");
}
else {
$_SESSION['user'] = getenv("REMOTE_HOST")." [".getenv("REMOTE_ADDR")."]";
}
$_SESSION['time'] = date("d-m-Y \a\\t G:i:s");
$fp =@fopen($filename,"ab") or
die ("error opening counter file");
if(!isset($_SESSION['repeat'])) {
$_SESSION['repeat']= "yes";
$message = "\n*".$_SESSION['user']." visited ";
$message .= getenv("SCRIPT_NAME");
$message .= " on: ".$_SESSION['time']."*";
@fwrite($fp,$message);
@fclose($fp);
}
?>