当我刷新网页时,评论在文件上被保存两次 [英] comments are being saved twice on a file when i refresh the webpage

查看:90
本文介绍了当我刷新网页时,评论在文件上被保存两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写我的网站的评论块。
我将评论保存在一个文件中并在网页上打印内容。
,但问题是当我刷新网页时,最后一条评论显示两次。



这里是我的代码:
i正在写评论块网站。
我将评论保存在一个文件中,并在网页上打印内容




但问题是当我刷新网页时,最后一个

评论显示两次。



这里是我的代码:

 < html> 


< body>

< form method =GET>

< textarea rows =15cols =50name =comments>
< / textarea>

< input type =submitvalue =submit>

< / form>

< / body>
< / html>如果(!($ _ GET [注释] == null)){




$ b<?php


b $ b $ comments =Anonymous said:< br>。$ _ GET

[comments]。< br>< br> ;
$ file_comments = fopen(comments.txt,a);
fwrite($ file_comments,$ comments);
fclose($ file_comments);

$ _GET [comments] = null;
$ comments = null;

}


$ comments = file_get_contents(comments.txt);
echo $ comments;


$ _GET [comments] = null;
$ comments = null;

?>


解决方案

以下是快速解决方案:


  1. 在您的表单被保存或出现错误后,将它们重定向到同一页面,但使用 GET uri中的变量,如 process.php?action = save 。使用标题功能进行重定向。

  2. 您还使用cookie保存提交表单的人员的IP并限制他在一段时间内能够提交该表格再次。


i am writing a comment block in my website. i save the comment in a file and print the contents on the webpage. but the problem is when i refresh the webpage the last comment gets displayed twice.

Here's my code: i am writing a comment block in my website. i save the comment in a file and print the contents on

the webpage. but the problem is when i refresh the webpage the last

comment gets displayed twice.

Here's my code:

<html>


<body>

<form   method="GET">

<textarea rows="15" cols="50" name="comments" >
</textarea>

<input type="submit" value="submit" >

</form>

</body>
</html>



<?php


if(!($_GET["comments"]==null)){
$comments = "Anonymous said:<br>".$_GET

["comments"]."<br><br><br><br>";
$file_comments = fopen("comments.txt","a");
fwrite($file_comments,$comments);
fclose($file_comments);

$_GET["comments"] = null;
$comments = null;

}


$comments = file_get_contents("comments.txt");
echo $comments;


$_GET["comments"] = null;
$comments = null;

?>

解决方案

Here are quick solutions:

  1. After your form has been saved or if it has an error, redirect them to the same page but with GET variables in the uri like process.php?action=save. Use header function for redirection.

  2. You also use cookies to save the IP of the person who submits the form and restrict him for certain period to be able to submit the form again.

这篇关于当我刷新网页时,评论在文件上被保存两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆