私は自分のウェブサイトにコメントブロックを書いています。コメントをファイルに保存し、内容を印刷します
ウェブページ。しかし問題は、最後にウェブページを更新したときです
コメントが 2 回表示されます。
つまり、コメントデータベースファイルの最後のコメントを検索し、見つかった場合はファイルに書き込みません。
これが私のコードです
<html>
<body>
<form method="GET">
<textarea rows="15" cols="50" name="comments" ></textarea>
<input type="submit" value="submit" >
</form>
</body>
</html>
<?php
$refresh = 0;
$comments = file_get_contents("comments.txt");
$file_comments_len = strlen($comments);
$current_comments = $_GET["comments"];
$current_comments_len = strlen($current_comments);
for($i = 0; $i<= $file_comments_len; $i++){
$sub = substr($comments, $i, (($i+$current_comments_len-1 )) );
echo $sub."<br>";
echo $i;
if( $sub == $comments){
$refresh = 100;
break;
}
}
if( ($_GET["comments"]!=null) && ($refresh==0) ){
$comments = /*"Anonymous said:<br>".*/$_GET["comments"]."<br><br>";
//$file_comments = fopen("comments.txt","a");
//fwrite($file_comments,$comments);
//fclose($file_comments);
file_put_contents( "comments.txt", $comments, FILE_APPEND );
$_GET["comments"] = null;
$comments = null;
}
//$file_comments2 = fopen("comments.txt", "r");
$comments = file_get_contents("comments.txt");
echo $comments;
//fclose($file_comments2);
$_GET["comments"] = null;
$comments = null;
?>