prattmic Posted August 10, 2006 Share Posted August 10, 2006 I have text logs that I wish to auto-prune. This is the header of the log:[code=php:0]<?php// DO NOT DELETE THISinclude '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! Quote Link to comment Share on other sites More sharing options...
Jocka Posted August 10, 2006 Share Posted August 10, 2006 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. Quote Link to comment Share on other sites More sharing options...
prattmic Posted August 10, 2006 Author Share Posted August 10, 2006 Well, I'm kinda new to PHP, so I get what your saying, but I don't know how to do it. Quote Link to comment Share on other sites More sharing options...
Jocka Posted August 10, 2006 Share Posted August 10, 2006 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] Quote Link to comment Share on other sites More sharing options...
prattmic Posted August 10, 2006 Author Share Posted August 10, 2006 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. Quote Link to comment Share on other sites More sharing options...
Jocka Posted August 10, 2006 Share Posted August 10, 2006 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? Quote Link to comment Share on other sites More sharing options...
prattmic Posted August 11, 2006 Author Share Posted August 11, 2006 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. Quote Link to comment Share on other sites More sharing options...
Jocka Posted August 11, 2006 Share Posted August 11, 2006 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. Quote Link to comment Share on other sites More sharing options...
prattmic Posted August 11, 2006 Author Share Posted August 11, 2006 Yes, except 20 is the oldest. 19 is [code=php:0]?>[/code] Quote Link to comment Share on other sites More sharing options...
prattmic Posted August 11, 2006 Author Share Posted August 11, 2006 Um, are you going to give me the code? Quote Link to comment Share on other sites More sharing options...
prattmic Posted August 12, 2006 Author Share Posted August 12, 2006 Hello? Quote Link to comment Share on other sites More sharing options...
prattmic Posted August 12, 2006 Author Share Posted August 12, 2006 ... ??? ??? ??? Quote Link to comment Share on other sites More sharing options...
Jocka Posted August 12, 2006 Share Posted August 12, 2006 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 Quote Link to comment Share on other sites More sharing options...
prattmic Posted August 12, 2006 Author Share Posted August 12, 2006 The script is making my log longer not shorter. Quote Link to comment Share on other sites More sharing options...
Jocka Posted August 12, 2006 Share Posted August 12, 2006 i made a small change to the code.. try it again Quote Link to comment Share on other sites More sharing options...
prattmic Posted August 13, 2006 Author Share Posted August 13, 2006 Now it got rid of the part that wasn't suppose to be deleted Quote Link to comment Share on other sites More sharing options...
Jocka Posted August 13, 2006 Share Posted August 13, 2006 ok i edited it again.. try again.. sry about all the problems, i'm just rushing through it lol Quote Link to comment Share on other sites More sharing options...
prattmic Posted August 13, 2006 Author Share Posted August 13, 2006 Now it deleted the whole log. Quote Link to comment Share on other sites More sharing options...
prattmic Posted August 13, 2006 Author Share Posted August 13, 2006 hello? Quote Link to comment Share on other sites More sharing options...
prattmic Posted August 13, 2006 Author Share Posted August 13, 2006 ... Quote Link to comment Share on other sites More sharing options...
prattmic Posted August 14, 2006 Author Share Posted August 14, 2006 bump Quote Link to comment Share on other sites More sharing options...
Jocka Posted August 14, 2006 Share Posted August 14, 2006 ok.. did some things again.. try it again. I really am sorry. I didn't want this to take this long either, i swear. Quote Link to comment Share on other sites More sharing options...
prattmic Posted August 14, 2006 Author Share Posted August 14, 2006 ok, Now it doubled everything and put an extra blank line between every line. Quote Link to comment Share on other sites More sharing options...
Jocka Posted August 14, 2006 Share Posted August 14, 2006 i dunno.. it should've worked.. i'm lost now.. Quote Link to comment Share on other sites More sharing options...
prattmic Posted August 14, 2006 Author Share Posted August 14, 2006 Do you want the log, and you can mess around with it? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.