2013-06-22 110 views
0

我需要在我的網站上創建最後登錄的成員列表,但我不想要用戶MySQL。我做了一些搜索在計算器谷歌寫了這樣的代碼:用PHP設置文本文件中添加內容的限制

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

問題是我不能設置限制!我如何編輯此代碼來設置限制,僅在文本文件中添加用戶?

+0

ü希望當限額達到20 – DevZer0

+0

20後,我想刪除文本文件的第一行和最後一行添加最後一個成員做什麼! – Alireza

+0

確定首先你只創建1行。其次是你的'preg_match'工作,因爲我沒有看到你的正則表達式有'「/../」' – DevZer0

回答

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); 
} 
} 
+0

謝謝你,我的朋友,我從你的答案中得到了一些想法。 – Alireza

+0

沒問題。很高興我能幫助你。 –