我知道这是一个非常常见的问题,但我找不到答案
我是一个post.php,它将一个帖子表单提交给controller.php 当它正确时,在控制器中它会:
header("Location: ./post?ok=1");
邮件实际插入,它通过上面的行, 但是浏览器中的网址似乎永远不会改变,最后还没有ok参数,
我可以使用会话变量来存储这样的成功/失败参数,但这种方式应该正常工作
post.php中:
<form action="controller" method="post">
<input name="test" value='test' type="text" />
<input type="submit" value="post" />
</form>
Controller.php这样:
<?php
header("Location: ./post?ok=1");
?>
编辑1:
header("Refresh:1;url=http://localhost/test/post?ok=1"); //doesn't work better
编辑2:
在post.php上面我放了
debug("post l ".count($_POST));
debug("get l ".count($_GET));
他们在提交前显示0,在
之后显示0edit3:它有效
被阻止:
if ($success){
header("Location: ./post?ok=1");
}
header("Location: ./post");
应该是:
if ($success){
header("Location: ./post?ok=1");
} else {
header("Location: ./post");
}
全部
答案 0 :(得分:3)
尝试使用HTTP状态代码303参见Oter:
header("Location: your/location", true, 303);
答案 1 :(得分:3)
首先,你不应该使用$_SERVER['HTTP_REFERER']
这是不安全的。此外,客户端可能无论如何都不会发送此标头。现在我不知道怎么做,但我确信这可以用来劫持你的网站以寻找邪恶的东西。
最好你应该知道表单数据的来源,并将用户重定向到那里。
顺便说一句,您的重定向可能无法按预期工作,因为该网址已包含问号。因此,您必须使用&
符号添加更多参数。
对于重定向,我会建议HTTP 302 Found
响应状态。但你必须确保之前没有其他任何东西发送出去。在header
行之后,必须没有HTML输出或空行:
header("Location: http://".$_SERVER['HTTP_HOST']."/your/source.form", true, 302);
答案 2 :(得分:1)
服务器端可能存在一些问题(例如,您的代码在设置标头之前打印出一些内容)。为了对此进行调试,您需要在php.ini中设置display_errors = On
,或者使用ini_set('display_errors', 1)
在代码中暂时启用它。
在大多数情况下,错误为"Cannot modify header information - headers already sent."
。在PHP结束标记(print
)之后确保没有echo
,?>
或任何空格和额外行
答案 3 :(得分:1)
这可能是服务器第二次重定向的问题。
header('Location: post.php?ok=1', true, 303);
exit;
尝试将扩展 .php 添加到网址,以确保帖子?ok = 1 未重定向到 post.php < / strong>由服务器(例如htaccess)。在附加重定向中,您可能会丢失get参数。然后不要忘记在标题重定向后添加一个退出。
答案 4 :(得分:0)
实际上......看来你没有跟着“退出”跟随header()调用。 例子:
header( 'location: ' . $location ); exit;
header( 'location: ' . $location, true, 301 ); exit;
在通话后“退出”时,会在浏览器地址栏中更改URL,因为会阻止代码继续执行。否则,代码继续执行,这会导致URL保持不变。