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! Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/ 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. Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-72779 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. Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-72782 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] Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-72783 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. Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-72784 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? Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-72786 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. Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-72789 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. Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-72792 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] Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-72793 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? Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-73204 Share on other sites More sharing options...
prattmic Posted August 12, 2006 Author Share Posted August 12, 2006 Hello? Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-73683 Share on other sites More sharing options...
prattmic Posted August 12, 2006 Author Share Posted August 12, 2006 ... ??? ??? ??? Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-73852 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 Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-73863 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. Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-73876 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 Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-73882 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 Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-73889 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 Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-73890 Share on other sites More sharing options...
prattmic Posted August 13, 2006 Author Share Posted August 13, 2006 Now it deleted the whole log. Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-73898 Share on other sites More sharing options...
prattmic Posted August 13, 2006 Author Share Posted August 13, 2006 hello? Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-74201 Share on other sites More sharing options...
prattmic Posted August 13, 2006 Author Share Posted August 13, 2006 ... Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-74240 Share on other sites More sharing options...
prattmic Posted August 14, 2006 Author Share Posted August 14, 2006 bump Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-74508 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. Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-74568 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. Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-74647 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.. Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-74682 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? Link to comment https://forums.phpfreaks.com/topic/17184-auto-prune-log/#findComment-74785 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.