Note that there are some explanatory texts on larger screens.

plurals
  1. POlast comment gets saved twice on my file and therefore gets printed on the webpage twice
    text
    copied!<p>i am writing a comment block in my website. i save the comment in a file and print the contents on</p> <p>the webpage. but the problem is when i refresh the webpage the last</p> <p>comment gets displayed twice.</p> <p>so i have used a logic, that is i search the last comment in my comment database file and if it finds it does not write on the file.</p> <p>Here's my code</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;form method="GET"&gt; &lt;textarea rows="15" cols="50" name="comments" &gt;&lt;/textarea&gt; &lt;input type="submit" value="submit" &gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; &lt;?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&lt;= $file_comments_len; $i++){ $sub = substr($comments, $i, (($i+$current_comments_len-1 )) ); echo $sub."&lt;br&gt;"; echo $i; if( $sub == $comments){ $refresh = 100; break; } } if( ($_GET["comments"]!=null) &amp;&amp; ($refresh==0) ){ $comments = /*"Anonymous said:&lt;br&gt;".*/$_GET["comments"]."&lt;br&gt;&lt;br&gt;"; //$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; ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload