Jump to content

Archived

This topic is now archived and is closed to further replies.

prattmic

Auto-Prune Log

Recommended Posts

I have text logs that I wish to auto-prune.  This is the header of the log:
[code=php:0]<?php
// DO NOT DELETE THIS
include 'phpsimplechoose_config.php';
//Below is where the verification takes place. Try to play around with it.
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Please enter User/Password"');
header('HTTP/1.0 401 Unauthorized');
die;

} else {

if (($_SERVER['PHP_AUTH_USER'] !== $phpsc_username) || ($_SERVER['PHP_AUTH_PW'] !== $phpsc_password)) {
header('WWW-Authenticate: Basic realm="Incorrect! Please try again."');
header('HTTP/1.0 401 Unauthorized');
die;
}
}
// To delete log erase everything after the next line, but not the next line itself (Line 20 and down can be deleted)
?>[/code]

The rest of the log consists of lines looking somewhat like this:
[code]192.168.1.1: Thu August 10, 2006 11:23 : Choice 1.  thgfd  Choice 2.  hfghfghd  Choice 3.  fgdgfh  We say... hfghfghd <br />[/code]

What I want to do is when the log reaches X number of the previous entries for it to delete some.  However, I want the header not to be deleted.

If you need more info, just ask!

Thanks in advance!

Share this post


Link to post
Share on other sites
you could just use some file functions to count the lines and if you have so many, delete from wherever to wherever... ya know what i'm saying? lol.

Share this post


Link to post
Share on other sites
alright googled and found ya something. use something like this

[code]<?
  $file = "logs.php";

  // NUMBER OF LINES
  $lines = count(file($file));

  // This will look for 50 or more entries if logs start at line 20 (19 in array)
  if($lines >= '69'){
 
      // CODE TO ERASE FIRST 20 or 30 I SUPPOSE
      // ... I'm looking for some code now lol..
       
  }
 
?>[/code]

Share this post


Link to post
Share on other sites
I wouldn't want to delete first 19 lines as that would get rid of my header in the log.  And I would want to delete enough so that it is under its limit of lines.

Share this post


Link to post
Share on other sites
that wouldn't delete the first 19 lines. I'm just calculating, if the line number for the first log is 19 then 69 makes the log 50 lines long. There for there are 50 entries.
Whats your limit, like how many do you want on there? 10,30,50,100? I think i might have found the code you need. But I also need to know how your log is ordered. 20th line being the newest or oldest?

Share this post


Link to post
Share on other sites
Line 20 is the first line of my log and it is the oldest entry.  Well, since this is for a script that I distribute, the limit would vary meaning it would need some kind of code to add 19/20 to however high the limit is.

Share this post


Link to post
Share on other sites
oh so the limit isn't really SET, its whatever the person wants.. right.. ok. 19 being oldest, you'd need to delete enough to keep the limit.

As in, if there is a limit of 100 and you have 104, you only need to delete the first 4.

alright, let me see what i can find here.

Share this post


Link to post
Share on other sites
alright. i wrote something quick with what i gave u yesterday and some other code. you should look over it and fix / edit everything you need to
[code]
<?
$file = "logs.php";
$limit = "50";
$text = NULL;

// NUMBER OF LINES
$logs = file($file);
$lines = count($logs);

$line_count = $lines - 18;

// This will look for 50 or more entries if logs start at line 20 (19 in array)
if($line_count > $limit){

    $line_count = $line_count - 50;
    $line_count = $line_count + 18;

    if (!$handle = fopen($file, 'w')) {
      echo "Cannot open file ($file)";
      exit;
    }

    foreach($logs as $line_num => $line){

        if($line_num <= '18' || $line_num > $line_count){

          $text .= $line . "\n";

        }
    }

    if (fwrite($handle, $text) === FALSE) {
      echo "Cannot write to file ($file)";
      exit;
    }
 
    fclose($handle);
 
}
 
?>
[/code]
let me fix this real quick

Share this post


Link to post
Share on other sites
i made a small change to the code.. try it again

Share this post


Link to post
Share on other sites
ok i edited it again.. try again.. sry about all the problems, i'm just rushing through it lol

Share this post


Link to post
Share on other sites
ok.. did some things again.. try it again. I really am sorry. I didn't want this to take this long either, i swear.

Share this post


Link to post
Share on other sites
i dunno.. it should've worked.. i'm lost now..

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.