当我试图从我在第一个脚本中设置的$_REQUEST[]
数组中检索变量status
(然后进行重定向)时,我只看到一个警告Undefined index: status
。为什么呢?
<?php
$_REQUEST['status'] = "success";
$rd_header = "location: action_script.php";
header($rd_header);
?>
行动script.php
<?php
echo "Unpacking the request variable : {$_REQUEST['status']}";
这是因为您的header()
语句将用户重定向到一个全新的URL。任何$_GET
或$_POST
参数都不再存在,因为我们不再在同一页面上。
你有几个选择。
1-首先,您可以使用$_SESSION
在页面重定向中保存数据。
session_start();
$_SESSIONJ['data'] = $data;
// this variable is now held in the session and can be accessed as long as there is a valid session.
2-在重定向时为URL添加一些get参数-
$rd_header = "location: action_script.php?param1=foo¶m2=bar";
header($rd_header);
// now you'll have the parameter `param1` and `param2` once the user has been redirected.
对于第二个方法,这个文档可能很有用。这是一个从名为http_build_query()
的数组中创建查询字符串的方法。
您要查找的是会话:
<?php
session_start();
$_SESSION['status'] = "success";
$rd_header = "location: action_script.php";
header($rd_header);
?>
<?php
session_start();
echo "Unpacking the request variable : {$_SESSION['status']}";
注意在两个页面的顶部都添加了session_start()
。正如你在我发布的链接中所读到的,这是必需的,必须在你希望使用会话的所有页面上。
您正在寻找的可能是发送一个GET参数:
$rd_header = "Location: action_script.php?status=success";
header($rd_header);
可以在action_script.php
中通过:
$_GET['status'];
在这种情况下,您实际上不需要会话或cookie,但您必须考虑用户可以轻松编辑GET帖子的事实。