PHP header() redirect with POST variables
If you don't want to use sessions, the only thing you can do is POST to the same page. Which IMO is the best solution anyway.
// form.php
<?php
if (!empty($_POST['submit'])) {
// validate
if ($allGood) {
// put data into database or whatever needs to be done
header('Location: nextpage.php');
exit;
}
}
?>
<form action="form.php">
<input name="foo" value="<?php if (!empty($_POST['foo'])) echo htmlentities($_POST['foo']); ?>">
...
</form>
This can be made more elegant, but you get the idea...
Is it possible to send POST data with a PHP redirect?
Long story short: no, it's not possibile using PHP.
One way I can think of doing this is to output the new form output as hidden fields along with javascript to auto-submit the form ...
It's the only way to do it.
The question is a possible duplicate of PHP Redirect with POST data.
As a suicide solution, you may consider to use HTTP 307 - Temporary Redirect.
From the HTTP/1.1 documentation linked above:
If the 307 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
My bold simply emphasizes that the redirect without the user confirmation actually depends on the browser's implementation of the protocol.
The redirect can be implemented in the PHP script which receives the POST request as follows*:
header('HTTP/1.1 307 Temporary Redirect');
header('Location: <your page in an external site>');
Basic usage Example:
page1.html - the page which contains the form
<html>
<body>
<form method="POST" action="page2.php">
<input name="variable1" />
<input name="variable2" />
<input type="submit" />
</form>
</body>
</html>
page2.php - the page which processes your POST data before redirect
<?php
if (isset($_POST['variable1']) && isset($_POST['variable2'])) {
//do your processing stuff
header('HTTP/1.1 307 Temporary Redirect');
header('Location: page3.php');
exit;
} else
echo 'variable1 and variable2 are not set';
page3.php the page to which the POST data has to be redirected (i.e. the external site)
<?php
if (isset($_POST['variable1']) && isset($_POST['variable2'])) {
foreach ($_POST as $key=>$val)
echo $key . ' = ' . $val . '<br>';
} else
echo 'variable1 and variable2 are not set';
* don't try this at home!
PHP Header Redirect with parameter
For send variable
$user = 'test';
Header("Location: query.php?user=".$user);
For receive the value of user variable
$user = $_REQUEST['user'];
I hope it will help to resolve your problem.
PHP Redirect with POST data
Generate a form on Page B with all the required data and action set to Page C and submit it with JavaScript on page load. Your data will be sent to Page C without much hassle to the user.
This is the only way to do it. A redirect is a 303 HTTP header that you can read up on http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html, but I'll quote some of it:
The response to the request can be
found under a different URI and SHOULD
be retrieved using a GET method on
that resource. This method exists
primarily to allow the output of a
POST-activated script to redirect the
user agent to a selected resource. The
new URI is not a substitute reference
for the originally requested resource.
The 303 response MUST NOT be cached,
but the response to the second
(redirected) request might be
cacheable.
The only way to achieve what you're doing is with a intermediate page that sends the user to Page C. Here's a small/simple snippet on how you can achieve that:
<form id="myForm" action="Page_C.php" method="post">
<?php
foreach ($_POST as $a => $b) {
echo '<input type="hidden" name="'.htmlentities($a).'" value="'.htmlentities($b).'">';
}
?>
</form>
<script type="text/javascript">
document.getElementById('myForm').submit();
</script>
You should also have a simple "confirm" form inside a noscript tag to make sure users without Javascript will be able to use your service.
PHP Redirection with Post Parameters
You CAN header redirect a POST request, and include the POST information. However, you need to explicitly return HTTP status code 307. Browsers treat 302 as a redirect with for GET, ignoring the original method. This is noted explicitly in the HTTP documentation:
- https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8
Practically, this means in PHP you need to set the status code before the redirect location:
header('HTTP/1.1 307 Temporary Redirect');
header('Location: anotherpage.php');
However, note that according to the HTTP specification, the user agent MUST ask user if they are ok resubmitting the POST information to the new URL. In practical terms, Chrome doesn't ask, and neither does Safari, but Firefox will present the user with a popup box confirming the redirection. Depending on your operating constraints, maybe this is ok, although in a general usage case it certainly has the potential to cause confusion for end users.
PHP Header refresh with post variables
The Boolean Flag TRUE OR FALSE in the header()
determines whether you want the current header()
to replace a previous one. If you want multiple headers, set it to FALSE. By default, this is TRUE... This means any subsequent header()
replaces the Previous One. The 3rd Parameter is the HTTP RESPONSE CODE.
To explicitly set the Response Code to 307, You can use PHP http_response_code($code)
. Combining the 2, You can have something like this:
// EXPLICITLY SET RESPONSE CODE TO 307
http_response_code(307);
// REFRESH & REDIRECT TO page.php
// IN FACT; THERE IS NO NEED FOR THE http_response_code(307) ABOVE
// THIS ONE LINE DOES IT ALL IN ONE GO...
header("refresh:2; url=page.php", FALSE, 307);
If you increased the refresh
to (say) 15 so that you have enough time to look into the the Browser's Web-Developer Tools
(under the Networks Tab
), You'd notice that the HTTP Response Code of 307
was indeed set & sent....
To pass some data from the current page to page.php
, You have 2 Options. Pass the Data via $_GET
like so:
<?php
$url = urlencode("page.php?user=web&password=developper&city=bronx");
header("refresh:2; url={$url}", FALSE, 307);
Or You could use Session like so:
<?php
//FIRST CHECK IF SESSION EXIST BEFORE STARTING IT:
if (session_status() == PHP_SESSION_NONE || session_id() == '') {
session_start();
}
// SET SOME DATA INTO THE $_SESSION (TO BE USED IN page.php, ETC.)
$_SESSION['user'] = 'web';
$_SESSION['pass'] = 'developper';
$_SESSION['city'] = 'bronx';
header("refresh:2; url=page.php", FALSE, 307);
PHP - Redirect and send data via POST
You can't do this using PHP.
As others have said, you could use cURL - but then the PHP code becomes the client rather than the browser.
If you must use POST, then the only way to do it would be to generate the populated form using PHP and use the window.onload hook to call javascript to submit the form.
Post data in header location with post method
try this auto submit form
<?php
if(isset($email))
{
?>
<form name="myForm" id="myForm" action="payment.php" method="POST">
<input name="email" value="<?php echo $email;?>" />
</form>
<script>
function submitform()
{
document.getElementById("myForm").submit();
}
window.onload = submitform;
</script>
<?php
}
?>
Related Topics
How to Override Trait Function and Call It from the Overridden Function
How to Use Mamp's Version of PHP Instead of the Default on Osx
Stop Caching for PHP 5.5.3 in Mamp
Best Practices: Working with Long, Multiline Strings in PHP
File_Get_Contents Behind a Proxy
Split String by Delimiter, But Not If It Is Escaped
Back to Previous Page with Header( "Location: " ); in PHP
PHP Messing with HTML Charset Encoding
Ssl Operation Failed with Code 1: Dh Key Too Small
PHP Emitting 500 on Errors - Where Is This Documented
MySQL Db Question Marks Instead of Hebrew Characters..
Regex Backreference to Match Different Values