Click to See Complete Forum and Search --> : Ok..Ok..Simple Question.
Ok, I have this little script here that counts the lines in my log file and then echos the number of lines:
<?PHP
$contents = @file("log.txt");
$num_lines = count($contents);
echo $num_lines;
?>
I have another script that is writing the log file and also clears the file after it gets to 20 lines. Is there a peice of code that I can add to the script above that will echo the time also. So it would display something like "12 lines since 10:27PM"? I'm not exactally sure if it can be done and I have searched hi and lo for a script like this. I would greatly appreciate any help. :D
[EDIT: Sorry forgot to also post that I want it to say "12 lines since 10:27PM" only when the log file is cleared. So the script would look at the file and when ever it has the number of 1 it will post the current time with the number of lines.]
brendandonhue
08-07-2003, 04:57 PM
Have the script that is writing the file count the lines also, and say something like if $num_lines=0, then write
date("F j, Y, g:i a")
to the file.
Could you or someone possibly help me with the coding? I really suck when it comes to PHP! I would greatly appreciate any help. Thanks. ;)
brendandonhue
08-08-2003, 08:43 AM
Can you post the script thats writing/clearing the log file?
Ok, here is my (online.php) file which writes each unique hit to the file called (log.txt) and also clears the file after it gets to 20 lines. It also writes to another file called (online.txt) which is for the users currently online:
<?PHP
$file = "online.txt"; #chmod to 666
$logfile = "log.txt"; #chmod to 666 -- used for log file
$minutes = "1"; # number of minutes until users are no longer online
$ip = $_SERVER['REMOTE_ADDR'];
$time = time();
$data = "";
$users = "0";
$found = "0";
#set up log file data
$logtime = date("h:ia");
$logdate = date("m/d/y");
$logdata = "($ip | $logtime | $logdate)\n";
if (!isset($_COOKIE["logged"])) { #if cookie is not set
#write log file
$fp = fopen($logfile, "a");
fwrite ($fp, $logdata);
fclose ($fp);
setcookie ("logged", "visited", time() +"1200"); #set for 20 minutes (time is in seconds)
}
$lines = @file($file);
$contents = @split("\|", $lines[0]); #split the first line of the file
if (count($lines) == 0) { #if file is empty
$x = 0;
}
else { #if there is already data in the file
$x = 1;
}
$fp = fopen($file, "w");
if ($x == 1) {
foreach ($contents as $content) { #loop through the users
list ($savedip,$savedtime) = split(":", $content); #list the users ip's and last time on page
if ($ip == $savedip) { #user already online, update time
$data .= "$ip:$time|";
$users++;
$found = "1";
}
else {
if ($time < $savedtime + ($minutes * 60)) { #other users online, don't update time
$data .= "$savedip:$savedtime|";
$users++;
}
}
}
}
if ($found == 0 or $x == 0) { #first time this user came online
$data .= "$ip:$time|";
$users++;
}
$data = substr($data, 0, -1); #cut off the trailing |
fwrite($fp, $data); #write the file
fclose ($fp);
$file = "log.txt"; #file to open/write to
$lines = "20"; #number of lines to read
$data = "";
$content = @file($file);
foreach ($content as $line_num => $line) {
if ($line_num < $lines) {
$data .= $line;
}
else {
$data = "";
}
}
$fp = fopen($file, "w");
fwrite($fp, $data); #write the file
fclose ($fp);
?>
This script was created with great help from Pyro so he will probably know more about it than I if you have any very technical questions.
brendandonhue
08-08-2003, 09:21 AM
I don't think it will work but you can try this. If not, you will have to wait for someone else
<?PHP
$file = "online.txt"; #chmod to 666
$logfile = "log.txt"; #chmod to 666 -- used for log file
$minutes = "1"; # number of minutes until users are no longer online
$ip = $_SERVER['REMOTE_ADDR'];
$time = time();
$data = "";
$users = "0";
$found = "0";
#set up log file data
$logtime = date("h:ia");
$logdate = date("m/d/y");
$logdata = "($ip | $logtime | $logdate)\n";
if (!isset($_COOKIE["logged"])) { #if cookie is not set
#write log file
$fp = fopen($logfile, "a");
fwrite ($fp, $logdata);
fclose ($fp);
setcookie ("logged", "visited", time() +"1200"); #set for 20 minutes (time is in seconds)
}
$lines = @file($file);
$contents = @split("|", $lines[0]); #split the first line of the file
if (count($lines) == 0) { #if file is empty
$x = 0;
}
else { #if there is already data in the file
$x = 1;
}
$fp = fopen($file, "w");
if ($x == 1) {
foreach ($contents as $content) { #loop through the users
list ($savedip,$savedtime) = split(":", $content); #list the users ip's and last time on page
if ($ip == $savedip) { #user already online, update time
$data .= "$ip:$time|";
$users++;
$found = "1";
}
else {
if ($time < $savedtime + ($minutes * 60)) { #other users online, don't update time
$data .= "$savedip:$savedtime|";
$users++;
}
}
}
}
if ($found == 0 or $x == 0) { #first time this user came online
$data .= "$ip:$time|";
$users++;
}
$data = substr($data, 0, -1); #cut off the trailing |
fwrite($fp, $data); #write the file
fclose ($fp);
$file = "log.txt"; #file to open/write to
$lines = "20"; #number of lines to read
$data = "";
$date = date("F j, Y, g:i a");
$content = @file($file);
foreach ($content as $line_num => $line) {
if ($line_num < $lines && != 0) {
$data .= $line;
}
else if ($line_num == 0) {
$data .= $date . "\n" . $line;
}
else {
$data = "";
}
}
$fp = fopen($file, "w");
fwrite($fp, $data); #write the file
fclose ($fp);
?>
Thank you very much. I will give it a try. Maybe if Pyro is around he could give his comments or corrections for this code. Thanks again. :D
I tried it and when I go to my page (http://kd7pyo.infinitypages.com/) it gives me this line 71 error:
Parse error: parse error in /home/infini15/public_html/kd7pyo/online.php on line 71
Here is what my PHP editor says line 71 is:
if ($line_num < $lines && != 0) {
I'm not sure what the problem is?
Incorrect syntax. Try:
if ($line_num < $lines && $line_num != 0) {
Ok that fix that problem but now I'm getting:
Warning: Invalid argument supplied for foreach() in /home/infini15/public_html/kd7pyo/online.php on line 40
Here is line 40:
foreach ($contents as $content) { #loop through the users
brendandonhue
08-08-2003, 09:44 AM
oops my bad :o
I fixed line 71 but did you see my last post about line 40?