简体   繁体   English

重定向不停止重新提交?

[英]Redirect not stopping re-submission?

PHP 的PHP

    $thisfile=$_SERVER["REQUEST_URI"];

    if(isset($_POST['comment'])&&!empty($_POST['comment'])){
    if($id!=0){
    $comment=$_POST['comment'];

    if ($comment_query=mysql_query("INSERT INTO `photosite`.`comments` VALUES ('$id', '', '$firstname', '$surname', '$photo_id', '$comment', '$time')")){

    header('Location: photopage.php?photo_id='.$photo_id.'', true, 303);
    exit;

    }else {
    echo 'Could not add comment';
    }
    }else {
    echo'Please Log In to add a Comment';
    }
    }       

I do not get why my 我不明白为什么我的

header('Location: photopage.php?photo_id='.$photo_id.'', true, 303);
    exit;       

Still alowas the user to resubmit data? 还允许用户重新提交数据吗? This links to the same page because it is for adding comments so when a user adds a comment, the page refreshes to the same page. 这链接到同一页面,因为它用于添加评论,因此当用户添加评论时,页面将刷新到同一页面。 But if you refresh again, the content is re-submitted, any idea why? 但是,如果再次刷新,内容将重新提交,为什么?

UPDATE: I DO have stuff echoed out before all this code in the same document. 更新:我确实在同一文档中的所有这些代码之前回荡了一些东西。 Is that the problem? 那是问题吗?

Ok I changed the form action to another page to be processed. 好的,我将表单操作更改为另一个要处理的页面。 There were previous things outputting to the page which was the problem! 有以前的东西输出到页面上,这就是问题所在!

Try 尝试

...
header('Location: /photopage.php?photo_id='.$photo_id.'', true, 303);
...
 header('Location: photopage.php?photo_id='.$photo_id.'&added='.time());
    exit;

should work. 应该管用。 The code you provided should also work. 您提供的代码也应该起作用。 The header->location removes the POST contents, so if you refresh the page afterwards, it should not resubmit. header-> location会删除POST内容,因此,如果以后刷新页面,则不应重新提交。

Pressing the BACK button is a different story, you cannot solve that, unless you are using an unique token for every form request. 按下BACK按钮是另一回事,除非您为每个表单请求都使用唯一的令牌,否则您将无法解决该问题。

Example: 例:

 //check if the token in the session matches the token in the post
 if( isset( $_POST['token'], $_SESSION['token']) 
     && $_SESSION['token'] == $_POST['token'] ){
    //handle your post data
     [...]
 }

 //set the token
 $_SESSION['token'] = sha1(time().mt_rand());

 //process your form
 ?>
 [...]
 <input type='hidden' name='token' value='<?php echo $_SESSION["token"];?>'/>
 [...]

Ok, seems I have find out your problem.. I think Your error reporting is switched off, so You don't see the warning about headers, "Cannot modify header information – headers already sent ...". 好的,似乎我已经找到了您的问题。我认为您的错误报告已关闭,因此您不会看到有关标头的警告,“无法修改标头信息–标头已发送...”。
You cant send header or modify them, as there are sent, because of that You must turn on buffering. 您不能发送标头或修改标头,因为已发送标头,因此必须打开缓冲。 Use ob_start() on the top of Your code. 在代码顶部使用ob_start() It must look like this 看起来像这样

<?php
ob_start();
..

Don't use any whitespaces before <?php . <?php之前不要使用任何空格。

And remove the parameter true, 303 from Your header function.. 并从您的标头函数中删除参数true,303。

header('Location: photopage.php?photo_id='.$photo_id);
exit();

http://www.php.net/manual/en/function.ob-start.php http://www.php.net/manual/zh/function.ob-start.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM