English 中文(简体)
使用 $_POST 将 PHP 停止批注保存到张贴
原标题:PHP stop comment from keeping to post itself using $_POST

所以,我正在写一个评论脚本。 当你发表评论时, 它会很好地工作, 但我发现当你刷新页面时, 即使文本字段是空的, 但它仍然会张贴同样的注释。 我理解, 这是因为我已经将变量发送到 $_POST , 并且它只是将该值插入到数据库中, 但我如何避免这一问题? 感谢您, 并且这是我的代码 : (假设 $username $image 已经设定) 。

if (isset($_POST[ text ]) && !empty($_POST[ text ]))
        {
            $text = $_POST[ text ];
            $timeStamp = time();

            mysql_query("INSERT INTO comments VALUES ( $image , $username , $text , $timeStamp )");
        }

和HTML:

        <form method = "post" action = "/view.php?image=$image" />

            <input type = "text" name = "text" maxlength = "100" />
            <input type = "submit" value = "Add Comment" />

        </form>
最佳回答

最容易避免的办法是在数据库运作成功后重新定向:

...
mysql_query("INSERT INTO comments VALUES ( $image , $username , $text , $timeStamp )");
// error handling
header( Location: /some/where );

除此之外,你真的需要切换到PDO/已准备好的声明,以避免注射问题。

问题回答

您是否张贴在您正在查看的注释所在的页面上? 如果您可以张贴到 / a_ page_ where_i_submit_things.php 。 php 然后重置到注释所在的页面上。 我相信这将有效 。

简单方式:

保存到数据库后, 重新装入您的页面 :

header( Location: comment-form.php );

这将让浏览器“忘记” 提交表格 。

<% 1> 更正 方式:

生成 < a href=> "http://en.wikipedia.org/wiki/Cryptlogic_nonce" rel= "nofollow noreferrer" >nonce 并添加为您格式的隐藏输入。当表格提交时,请确定 $_POST[unce] 与您的脚本中的 $unce 相符。

< a href=" "https://stackoverflow.com/ questions/4144531/how-to- create-and-use-nonces" > 如何创建和使用nices





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签