5

我正在尝试使用标头函数通过 URL 传递变量作为重定向页面的一种方式。但是当页面被重定向时,它会传递实际的变量名,而不是与变量关联的值。我是 PHP 新手,不完全理解语法,所以任何关于正确方法的进一步解释将不胜感激。

header('location: index.php?id=".$_POST[ac_id]."&err=".$login."');
4

7 回答 7

18

你要:

header("Location: index.php?id=".$_POST['ac_id']."&err=".$login);

您在这个字符串中组合了'",这就是它无法正确插入变量的原因。在我上面的示例中,您严格打开字符串"并将变量与字符串连接起来。

于 2011-04-26T02:32:31.017 回答
2

引号中有引号。试试这个:

header('location: index.php?id=' . urlencode($_POST['ac_id']) . '&err=' . urlencode($login));

urlencode()函数负责处理 url 中的所有保留字符。

http_build_query()如果您认为 URL 中的变量不止一两个,我会改为使用。

header('Location: index.php?' . http_build_query(array(
    'id' => $_POST['ac_id'],
    'err' => $login
)));

此外,从技术上讲,您不能在位置标头中使用相对路径。虽然它适用于大多数浏览器,但根据 RFC,它是无效的。您应该包含完整的 URL。

于 2011-04-26T02:33:17.753 回答
1

尝试 SESSION 存储。标头用于重定向页面。如果您真的只想通过标头传递值,那么您就可以生成 url。header('location:destination.php?value1=1&value2=3'); 但这对 vars 来说不是一个好习惯。只需将值存储在 SESSION 全局变量中。B4 header() 重定向调用。@接收页面你必须测试,如果会话 val isset() n !empty() 那么 ... 否则 ...

希望这会有所帮助。

于 2014-02-06T11:41:22.793 回答
0

你可以试试这个

header("Location:abc.html?id=".$_POST['id']."id_2=".$var['id_2']);

如果有效,请告诉我。这只是一个例子。

于 2013-04-17T11:22:14.660 回答
0

就我而言,很多时候 header() 无法正常工作。我使用 window.location.href 而不是 header 函数。你可以这样试试

echo "<script> 
            window.location.href='index.php?id=".$_POST[ac_id]."&err=".$login';
      </script>";
于 2021-01-29T18:08:56.863 回答
-2
header('location: index.php?id='.$_POST['ac_id'].'&err='.$login);
于 2011-04-26T02:32:58.067 回答
-2

尝试这个:

header("location: index.php?id=$_POST[ac_id]&err=$login");

PHP 变量在双引号字符串中展开。

于 2011-04-26T02:34:53.360 回答