0

Hello,

I am making my first PHP using website and I have some things that I am writing to a log.txt. Everytime someone visits my website something gets written like this:

$dateTime           = date('Y/m/d G:i:s');

$fh = fopen('log.txt', 'a');
fwrite($fh, 'Date / Time: '."".$dateTime ."\n\n");
fclose($fh);

Now I would like to know, how to set a max file size for my log.txt to stop it from getting too big. For example; it'll auto delete the oldest content "block" of let's say 6 lines long and replace it with the new one after the file has exceeded (for example) 500 lines.

I couldn't find this problem online so I am very curious to how I would do this.

If you have any questions please let me know and I hope you can help me with this problem!

Buster
  • 446
  • 5
  • 16
  • 1
    Write to database instead. – u_mulder Oct 09 '19 at 11:22
  • If you are using a web server, this and MUCH MORE information can be viewed in the web servers logs – RiggsFolly Oct 09 '19 at 11:22
  • I am hosting my website on 000webhost. But I am asking this because a friend of mine send about 40K bots to my website that caused the log.txt to become over 130 000 lines long. – Buster Oct 09 '19 at 11:27
  • 1
    Looks like you are reinventing log-rotation. :-) You have a few options: write it yourself, or google a little on log-rotation – Erwin Moller Oct 09 '19 at 11:27
  • Hmh alright. I don't want to use composer, this is just a single user application and the website will not get any visitors. – Buster Oct 09 '19 at 11:31

4 Answers4

1

You can name the file with the date as a filename So basically for each day you will have another file

$dateTime = date('Y/m/d G:i:s');
//The file will have the name log_2019-10-09.txt
$fh = fopen('log_'.date('Y-m-d').'.txt', 'a');
fwrite($fh, 'Date/Time: '."".$dateTime ."\n\n");
fclose($fh);
sayalok
  • 882
  • 3
  • 15
  • 30
beasst
  • 109
  • 8
1

I suggest using "rotation log files" for this instead. Research on google about this. You will get some easy solutions for it.

For example How to configure logrotate with php logs

Chemaclass
  • 1,933
  • 19
  • 24
1

Here is an Example how to get Lines Count from File https://www.w3resource.com/php-exercises/php-basic-exercise-16.php

or you can try to get file Size:

if(filesize("log.txt") >= 5000){ echo "file to is large"; }

or

$content = file_get_contents("log.txt");
$array = explode("\n", $content);
$count = count($array);
if($count >= 500){
echo "file too large";
}
PYuriy
  • 23
  • 3
1

Please try this code. I have tested it it works fine. I use \r\n for line break so that your text file is more readable.

$dateTime = date('Y/m/d G:i:s');
$fh = fopen('log.txt', 'a');
fwrite($fh, 'Date / Time: ' . "" . $dateTime . "\r\n");
fclose($fh);

now you check if the number of lines in the file exceed your limit, then remove the old lines from the top the the file and enter new line on the top, else just enter new line.

$block = 5; //block consist of 5 lines
$remove_blocks = 10; //remove the number of blocks
$remove = $block * $remove_blocks; //totle line to remove

$line_limit = 20;
$content = file_get_contents("log.txt");
$array = explode("\r\n", $content);
$count = count($array);
if ($count >= $line_limit) {

    //Remove first few lines
    $array = array_slice($array, $remove);
    $new_data = implode("\r\n", $array);

    $fh = fopen('log.txt', 'w');
    fwrite($fh, $new_data . "\r\n");
    fclose($fh);
} else {

    $dateTime = date('Y/m/d G:i:s');
    $fh = fopen('log.txt', 'a');
    fwrite($fh, 'Date / Time: ' . "" . $dateTime . "\r\n");
    fclose($fh);

}

Edit: put this code in a new test.php adn experiment with it

<?php

$block = 2; //block consist of 5 lines
$remove_blocks = 1; //remove the number of blocks
$remove = $block * $remove_blocks; //totle line to remove

$line_limit = 5;
$content = file_get_contents("log.txt");
$array = explode("\r\n", $content);
$array = array_slice($array, -1);
$count = count($array);
if ($count >= $line_limit) {

    //Remove first few lines
    $array = array_slice($array, $remove);
    $new_data = implode("\r\n", $array);

    $fh = fopen('log.txt', 'w');
    fwrite($fh, $new_data . "\r\n");
    fclose($fh);

    $dateTime = date('Y/m/d G:i:s');
    $fh = fopen('log.txt', 'a');
    fwrite($fh, 'Date / Time: ' . "" . $dateTime . "\r\n");
    fclose($fh);

} else {

    $dateTime = date('Y/m/d G:i:s');
    $fh = fopen('log.txt', 'a');
    fwrite($fh, 'Date / Time: ' . "" . $dateTime . "\r\n");
    fclose($fh);

}

?>
Umer Abbas
  • 1,866
  • 3
  • 13
  • 19
  • How can I set max amount of lines before it deletes? I think it is $line_limit = 20; but it is at line 120 now and it hasn't deleted anything – Buster Oct 09 '19 at 12:43
  • `if ($count >= $line_limit) {` this line checks the limit and then `$array = array_slice($array, $remove)` this line is actually deletes the lines from array. i think you were previously using `\n\n` so that's why it's not behaving right, replace that with `\r\n` – Umer Abbas Oct 09 '19 at 13:19
  • I think that's not necessary, what ever you want to write into the file you just create a variable then concatenate all the line with that variable to make a single string then write that string to the file and at the end of the string concatenate the line break so that next time php can check where the line break was. – Umer Abbas Oct 09 '19 at 13:42
  • Okay yes, I'm a bit slow but that indeed works. Now what If I want to change the max lines? I changed it to 200 but it doesn't delete when it has 200+ lines. **Sorry I forgot to change the amount of blocks it deleted. Thanks a lot for your help I really appreciate it!** – Buster Oct 09 '19 at 13:45
  • `$line = "line 1 \r\n"; $line .= " line 2 \r\n"; $line .= " line 3 \r\n";` here i'm creating one string that has 3 lines fwrite($fh, $line . "\r\n");. – Umer Abbas Oct 09 '19 at 13:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/200611/discussion-between-omar-abbas-and-mysteriousduck). – Umer Abbas Oct 09 '19 at 13:48