0

我需要在我的网站上创建一个最后登录成员的列表,但我不想使用 MySQL。我在stackoverflowGoogle中进行了一些搜索并编写了以下代码:

////////////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);
}
}

问题是我不能设置限制!我如何编辑此代码以设置限制以在文本文件中仅添加20 个用户?

4

1 回答 1

2

也许你可以做到这一点?

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);
 }
}
于 2013-06-22T09:55:56.790 回答