I did some research on this topic, and there are some experts who have said that it is not possible, so I would like to ask for an alternative solution.
My situation
/**
* Redirect with POST data.
*
* @param string $url URL.
* @param array $post_data POST data. Example: ['foo' => 'var', 'id' => 123]
* @param array $headers Optional. Extra headers to send.
*/
public function redirect_post($url, array $data, array $headers = null) {
$params = [
'http' => [
'method' => 'POST',
'content' => http_build_query($data)
]
];
if (!is_null($headers)) {
$params['http']['header'] = '';
foreach ($headers as $k => $v) {
$params['http']['header'] .= "$k: $v\n";
}
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if ($fp) {
echo @stream_get_contents($fp);
die();
} else {
// Error
throw new Exception("Error loading '$url', $php_errormsg");
}
}
Here there is another approach that works for me:
if you need to redirect to another web page (user.php
) and includes a PHP variable ($user[0]
):
header('Location:./user.php?u_id='.$user[0]);
or
header("Location:./user.php?u_id=$user[0]");
I have another solution that makes this possible. It requires the client be running Javascript (which I think is a fair requirement these days).
Simply use an AJAX request on Page A to go and generate your invoice number and customer details in the background (your previous Page B), then once the request gets returned successfully with the correct information - simply complete the form submission over to your payment gateway (Page C).
This will achieve your result of the user only clicking one button and proceeding to the payment gateway. Below is some pseudocode
HTML:
<form id="paymentForm" method="post" action="https://example.com">
<input type="hidden" id="customInvoiceId" .... />
<input type="hidden" .... />
<input type="submit" id="submitButton" />
</form>
JS (using jQuery for convenience but trivial to make pure Javascript):
$('#submitButton').click(function(e) {
e.preventDefault(); //This will prevent form from submitting
//Do some stuff like build a list of things being purchased and customer details
$.getJSON('setupOrder.php', {listOfProducts: products, customerDetails: details }, function(data) {
if (!data.error) {
$('#paymentForm #customInvoiceID').val(data.id);
$('#paymentForm').submit(); //Send client to the payment processor
}
});
I'm aware the question is php
oriented, but the best way to redirect a POST
request is probably using .htaccess
, ie:
RewriteEngine on
RewriteCond %{REQUEST_URI} string_to_match_in_url
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^(.*)$ https://domain.tld/$1 [L,R=307]
Explanation:
By default, if you want to redirect request with POST data, browser redirects it via GET with 302 redirect
. This also drops all the POST data associated with the request. Browser does this as a precaution to prevent any unintentional re-submitting of POST transaction.
But what if you want to redirect anyway POST request with it’s data? In HTTP 1.1, there is a status code for this. Status code 307
indicates that the request should be repeated with the same HTTP method and data. So your POST request will be repeated along with it’s data if you use this status code.
SRC
There is a simple hack, use $_SESSION
and create an array
of the posted values, and once you go to the File_C.php
you can use it then do you process after that destroy it.
You can use sessions to save $_POST
data, then retrieve that data and set it to $_POST
on the subsequent request.
User submits request to /dirty-submission-url.php
Do:
if (session_status()!==PHP_SESSION_ACTIVE)session_start();
$_SESSION['POST'] = $_POST;
}
header("Location: /clean-url");
exit;
Then the browser redirects to and requests /clean-submission-url
from your server. You will have some internal routing to figure out what to do with this.
At the beginning of the request, you will do:
if (session_status()!==PHP_SESSION_ACTIVE)session_start();
if (isset($_SESSION['POST'])){
$_POST = $_SESSION['POST'];
unset($_SESSION['POST']);
}
Now, through the rest of your request, you can access $_POST
as you could upon the first request.