Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Pass POST variables with header()?

I'm trying to use the header() function to create a redirect. I would like to display an error message. Currently I'm sending the message as a parameter through the URL, however this makes it look quite ugly.

Is there a way to pass this value as a post variable instead?

Any advice appreciated.

Thanks.

like image 445
Dan Avatar asked Mar 15 '10 12:03

Dan


2 Answers

Dan, You could start and store a session in PHP then save the message as a session variable. This saves you from having to transfer the message in an HTTP request.

Manipulating Sessions

//Start the session
session_start();

//Dump your POST variables
$_SESSION['POST'] = $_POST;

//Redirect the user to the next page
header("Location: bar.php");

Now, within bar.php you can access those POST variables by re-initiating the session.

//Start the session
session_start();

//Access your POST variables
$temp = $_SESSION['POST'];

//Unset the useless session variable
unset($_SESSION['POST']);

To read more about sessions, check out: http://php.net/manual/en/function.session-start.php

like image 82
Seaux Avatar answered Sep 17 '22 16:09

Seaux


The header function is used to send HTTP response headers back to the user so actually you cannot use it to create request headers :(

One possibility is to use the CURL but I don't think it is worth of what you are doing.

like image 34
Sarfraz Avatar answered Sep 20 '22 16:09

Sarfraz