Jump to content

Auto-Prune Log


prattmic

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!
Link to comment
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]
Link to comment
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?
Link to comment
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.
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.