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;
仍然允许用户重新提交数据?这链接到同一页面,因为它用于添加评论,所以当用户添加评论时,页面会刷新到同一页。但如果你再次刷新,内容会被重新提交,你知道为什么吗?
更新:我DO在同一文档中的所有这些代码之前都有相应的内容。这就是问题所在吗?
好的,我将表单操作更改为另一个要处理的页面。之前有东西输出到页面,这就是问题所在!
尝试
...
header('Location: /photopage.php?photo_id='.$photo_id.'', true, 303);
...
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"];?>'/>
[...]
好吧,我好像发现了你的问题。。我认为您的错误报告已关闭,因此您没有看到有关标头的警告,"无法修改标头信息–标头已发送…"。
你不能发送标题或修改它们,因为已经发送了,因此你必须打开缓冲。使用代码顶部的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