Jump to content

How would I get this log to automatically clear or be limited to how many?


Recommended Posts

Hi,

 

I apologize if this is the wrong place to post this thread.

 

I have created a log file for a php proxy script. The log is being generated by using this code:

$proxytime = date('h:i:s A');
$proxydate = date('d-m-Y');
$stuff = $_SERVER['REMOTE_ADDR'].",".$url." , ".$proxydate." , ".$proxytime."\n";
$fp = fopen('log.txt',"a");
fwrite($fp,$stuff);
fclose($fp);

 

I need to know how I can get the log to be limited to only 100 entries or something and then the oldest log would disappear, if you know what I mean. How would I do such a thing?

 

Would it need to modify this code or make a shell? Please understand that I am a huge noob with php, shells, cronjobs, etc. lol and will probably need someones assistance who is willing to explain it to me.

 

Thank you. :)

Brian

Yes, I have a database. However the proxy doesn't use databases so...

 

If it is possible to some how limit it without using a database that would be awesome. :)

 

I did see a comment here: http://luiscosio.com/add-url-logging-to-phproxy-05b2#comment-14292

Would that work? I have no idea how to set it up as they didn't provide clear instructions. And would that limit it only to 100 entries?

That just moves all the log entries up a file.. like from file 1 to 2.. file 2 to 3 etc.. however frequently you set up the cron to run. There's nothing limiting the logs to 100 entries though.. I'm a little unsure myself about how you could do it..

  • 2 weeks later...
  • 2 weeks later...

You can use file to read the contents of the file into an array (each line is an element), and array_unshift to prepend an element to that array (insert the new line at the beginning, if you are wanting newest -> oldest) or array_push to put it on the end of the array (insert new line at end, if you are wanting oldest -> newest). 

 

From there, if this script is only being called every $x time, you can use array_slice to get the first or last 100 elements.  If the script is being triggered every request or something, you can use an array_shift/array_push combo to take first element off while putting new one on end, or use an array_pop/array_unshift combo to chop off the last element while putting new on at the beginning. 

 

From there, all you have to do is run a loop to write each element of the array back to the file (don't forget to add the line breaks to the element you are unshifting or pushing).

  • 2 weeks later...
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.