Php 当我刷新网页时,评论被保存在一个文件中两次
我正在我的网站上写评论栏。 我将评论保存在一个文件中,并将内容打印在网页上。 但问题是,当我刷新网页时,最后一条评论会显示两次 这是我的密码: 我正在我的网站上写评论栏。 我将评论保存在一个文件中,并将内容打印在 这个网页。 但问题是我上次刷新网页的时候 注释显示两次 这是我的密码:Php 当我刷新网页时,评论被保存在一个文件中两次,php,html,Php,Html,我正在我的网站上写评论栏。 我将评论保存在一个文件中,并将内容打印在网页上。 但问题是,当我刷新网页时,最后一条评论会显示两次 这是我的密码: 我正在我的网站上写评论栏。 我将评论保存在一个文件中,并将内容打印在 这个网页。 但问题是我上次刷新网页的时候 注释显示两次 这是我的密码: <html> <body> <form method="GET"> <textarea rows="15" cols="50" name="comments"
<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;
?>
以下是快速解决方案: 保存表单后或表单有错误时,将表单重定向到同一页面,但在uri中使用GET变量,如process.php?action=save。使用头函数进行重定向 您还可以使用cookies保存提交表单的人的IP,并在一定时间内限制他再次提交表单
以下是快速解决方案: 保存表单后或表单有错误时,将表单重定向到同一页面,但在uri中使用GET变量,如process.php?action=save。使用头函数进行重定向 您还可以使用cookies保存提交表单的人的IP,并在一定时间内限制他再次提交表单
解决方案是重定向到同一页。例如,这将起作用。试试看
<html>
<body>
<form method="POST">
<textarea rows="15" cols="50" name="comments"></textarea>
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>
<?php
if ( isset( $_POST[ 'submit' ] ) ) {
$TextArea = $_POST[ "comments" ];
$comments = "Anonymous said:<br>" . $TextArea . "<br><br><br><br>\n\n"; // Add \n\n to write the comments in a different paragraph inside the file.
$file_comments = file_put_contents( "comments.txt", $comments, FILE_APPEND );
echo '<script type="text/javascript">window.location ="";</script>';
}
$comments = file_get_contents( "comments.txt" );
echo $comments;
?>
不过,刷新需要更多时间。最好的方法是将PHP脚本放在另一个文件中。解决方案是重定向到同一页面。例如,这将起作用。试试看
<html>
<body>
<form method="POST">
<textarea rows="15" cols="50" name="comments"></textarea>
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>
<?php
if ( isset( $_POST[ 'submit' ] ) ) {
$TextArea = $_POST[ "comments" ];
$comments = "Anonymous said:<br>" . $TextArea . "<br><br><br><br>\n\n"; // Add \n\n to write the comments in a different paragraph inside the file.
$file_comments = file_put_contents( "comments.txt", $comments, FILE_APPEND );
echo '<script type="text/javascript">window.location ="";</script>';
}
$comments = file_get_contents( "comments.txt" );
echo $comments;
?>
不过,刷新需要更多时间。最好的方法是将PHP脚本放在另一个文件中。您应该为此使用数据库。您应该为此使用数据库。我已经解决了我的问题。在我的计算机上运行.php文件的软件一定有问题。当我上传文件时,它工作得很好。我已经解决了我的问题。在我的计算机上运行.php文件的软件一定有问题。当我上传文件时,它工作得很好。