我有一个真正的问题,这是语法问题。通常,当我需要重定向任何页面时,我只使用标题功能。有时我在下面需要时使用变量:
if(condition) header("Location: home.php?wrong=1");
有时它可能是:
if(condition) header("Location: home.php?wrong=1&&success=2");
直接使用变量(错误或成功)没有问题。当我需要通过post方法从html表单重定向一个值时,我使用这个代码并且它可以工作:
if (condition) header("Location: home.php?id=".$_POST['topic_id']);
但我需要更多,我也需要传递另一个值。所以我用:
if (condition) header("Location: home.php?id=".$_POST['topic_id']&&wrong=1);
而这次我变得愚蠢。任何人都可以通过提供正确的语法帮助我吗?我只需要传递WRONG = 1部分。
答案 0 :(得分:5)
你需要:
if (condition) header("Location: home.php?id=" . $_POST['topic_id'] . "&wrong=1");
或
if (condition) header("Location: home.php?id={$_POST['topic_id']}&wrong=1");
答案 1 :(得分:1)
if (condition) {
$_SESSION['error']['topicId'] = $_POST['topic_id'];
$_SESSION['error']['otherData'] = array('sadfasdfasdfasd' => 'adsfasdf');
header("Location: home.php?wrong=1");
exit;
}
然后:
//home.php
if (!empty($_SESSION['error'])) {
$topicId = $_SESSION['error']['topicId'];
//... do someth ...
}
答案 2 :(得分:0)
如果PHP标头功能有任何问题,您可以记住,您可以在脚本HTML输出中打印出刷新元标记。
<meta http-equiv="refresh" content="0;url=http://yoursite.com/thepath/you/want/file.php?parames=anything">
这是关于元刷新标记的资源:
http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm