0

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';
    }
    }       

我不明白为什么我的

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

还在等待用户重新提交数据吗?这链接到同一页面,因为它用于添加评论,因此当用户添加评论时,页面会刷新到同一页面。但是如果再次刷新,内容会重新提交,知道为什么吗?

更新:在同一个文档中的所有这些代码之前,我确实有一些东西回显。那是问题吗?

好的,我将表单操作更改为要处理的另一个页面。有以前的东西输出到页面,这是问题!

4

3 回答 3

0

Try

...
header('Location: /photopage.php?photo_id='.$photo_id.'', true, 303);
...
于 2013-03-05T22:44:00.343 回答
0
 header('Location: photopage.php?photo_id='.$photo_id.'&added='.time());
    exit;

应该管用。您提供的代码也应该可以工作。header->location 删除了 POST 内容,因此如果您之后刷新页面,则不应重新提交。

按 BACK 按钮是另一回事,您无法解决这个问题,除非您为每个表单请求使用唯一令牌。

例子:

 //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"];?>'/>
 [...]
于 2013-03-05T22:55:48.383 回答
0

好的,看来我已经找到了您的问题.. 我认为您的错误报告已关闭,因此您看不到有关标头的警告,“无法修改标头信息 - 标头已发送...”。
您不能发送标头或修改它们,因为已发送,因此您必须打开缓冲。ob_start()在您的代码顶部使用。它必须看起来像这样

<?php
ob_start();
..

之前不要使用任何空格<?php

并从您的标头函数中删除参数 true, 303..

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

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

于 2013-03-05T23:07:52.233 回答