开发者

Does "header()" php function unset global variables?

开发者 https://www.devze.com 2023-04-01 03:40 出处:网络
I am developing a php script which contains an html form. If not all fields are filled in the correct way the script will signal an error and redirect back to the same page with the header function s

I am developing a php script which contains an html form.

If not all fields are filled in the correct way the script will signal an error and redirect back to the same page with the header function setting an error variable to yes with the get method:

header("Location: registration_page.php?error_empty=yes");

my script has an error handling part in which it highlights the fields containing a mistake, but I would like to keep the value of the fields correctly filled开发者_C百科.

I am implementing this feature as I found in this other question:

How can I keep a value in a text input after a submit occurs?

but the problem is that when the page reopens the forms will not contain the old values.

My question is: does anybody know if the header function unsets global variables in the $_REQUEST array?

And do you know what kind of solution could I adopt?Maybe sessions?

Thanks in advance,

Matteo!


$_COOKIES will stay set, but $_POST & $_GET will be destroyed, as the client is moving to a new page. If they need to be retained, they must first be stored into $_SESSION before calling the redirect.

session_start();
$_SESSION['last_post'] = $_POST;
header("Location: http://example.com");
exit();

// On the redirected page, use the stored POST values and unset them in $_SESSION
session_start();
if (empty($_POST) && isset($_SESSION['last_post'])) {
  $post = $_SESSION['last_post'];
  unset($_SESSION['last_post']);
}
else $post = $_POST;


Does anybody know if the header function unsets global variables in the $_REQUEST array?

No, it doesn't. Cookies ($_COOKIE) will remain.

Obviously $_GET will contain whatever you have in the redirect (eg: $_GET['error_empty'] = 'yes') and$_POST` will be empty because you aren't posting.

So it $_REQUEST will be a combination of $_COOKIE and the new $_GET parameters you set.


You probably shouldn't be using $_REQUEST anyway. Specify exactly where you expect your request parameters to be...


It doesn't, but it does make a new request. A new request means a new $_REQUEST which does not necessarily have all of the old data. $_COOKIE will still be there but $_GET and $_POST will be new meaning that $_REQUEST will reflect that. ($_FILES will also be empty and while it is not in `$_REQUEST, it is another user-supplied value will be reset).

If you want to restore the form, you'll need to either put all of the variables in the url in the header, or use $_SESSION or setcookie and then restore from $_GET, $_SESSION or $_COOKIES.

0

精彩评论

暂无评论...
验证码 换一张
取 消