Q:

Set limits for add content in text file with PHP

I need to create a list of last logged members in my website, but i don't wanna user MySQL. i did some search in stackoverflow and Google and wrote this code:
////////////CHECK LIMIT
$storage = "engine/data/write.txt";
$readz = file_get_contents($storage);
$listz = explode('|||',$readz);
$counter = count( $listz );
if($counter > "3"){
  $stop = "1";
}else{
  $stop = "0";
}

if($stop == "0"){
///REMOVE MEMBER IF AVAILABLE
if ( preg_match("<!-- UserID: {$member_id} -->", $readz)){
$content = file_get_contents($storage);
$content = str_replace("<!-- UserID: {$member_id} -->".$user."|||", '', $content);
file_put_contents($storage, $content);
}

//ADD MEMBER AGAIN
if ( !preg_match("<!-- UserID: {$member_id} -->", $readz)){
$beonline_file = fopen($storage, "a+");
fwrite($beonline_file, "<!-- UserID: {$member_id} -->".$user."|||");
fclose($beonline_file);
}
}

Problem is i can't set limit! how i can edit this code to set limit to add only 20 users in text file?

A:

Maybe you can do this?
if ( !preg_match("<!-- UserID: {$member_id} -->", $readz)){
 $nlistz = explode('|||',$readz);
 if(count( $nlistz ) == 20){
     array_shift($nlistz);
     $newlistz = implode("|||",$nlistz);
     $beonline_file = fopen($storage, "w+");
     fwrite($beonline_file, $newlistz."|||<!-- UserID: {$member_id} -->".$user."|||");
     fclose($beonline_file);
 }else{
     $beonline_file = fopen($storage, "a+");
     fwrite($beonline_file, "<!-- UserID: {$member_id} -->".$user."|||");
     fclose($beonline_file);
 }
}

