Jump to content


Photo

Auto-Prune Log


  • Please log in to reply
32 replies to this topic

#1 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 10 August 2006 - 11:33 PM

I have text logs that I wish to auto-prune.  This is the header of the log:
<?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)
?>

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

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!

#2 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 10 August 2006 - 11:38 PM

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.

#3 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 10 August 2006 - 11:43 PM

Well, I'm kinda new to PHP, so I get what your saying, but I don't know how to do it.

#4 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 10 August 2006 - 11:46 PM

alright googled and found ya something. use something like this

<?
  $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..
        
  }
  
?>


#5 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 10 August 2006 - 11:53 PM

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.

#6 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 10 August 2006 - 11:57 PM

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?

#7 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 11 August 2006 - 12:03 AM

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.

#8 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 11 August 2006 - 12:09 AM

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.

#9 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 11 August 2006 - 12:10 AM

Yes, except 20 is the oldest. 19 is
?>


#10 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 11 August 2006 - 05:23 PM

Um, are you going to give me the code?

#11 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 12 August 2006 - 02:40 PM

Hello?

#12 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 12 August 2006 - 10:01 PM

...  ??? ??? ???

#13 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 12 August 2006 - 10:36 PM

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
<?
$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);
   
}
  
?>
let me fix this real quick

#14 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 12 August 2006 - 11:12 PM

The script is making my log longer not shorter.

#15 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 12 August 2006 - 11:52 PM

i made a small change to the code.. try it again

#16 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 13 August 2006 - 12:20 AM

Now it got rid of the part that wasn't suppose to be deleted

#17 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 13 August 2006 - 12:22 AM

ok i edited it again.. try again.. sry about all the problems, i'm just rushing through it lol

#18 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 13 August 2006 - 01:04 AM

Now it deleted the whole log.

#19 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 13 August 2006 - 08:38 PM

hello?

#20 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 13 August 2006 - 11:01 PM

...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users