PHP Redirection With Post Parameters

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 - 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.

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!

Redirect with array using POST

You would be better off putting the array in a session and then redirecting without GET parameters and then accessing the session variables in the next page.

You can POST with JavaScript, though this is from years ago and there is undoubtedly a better way now:

function http_post_redirect($url='', $data=array(), $doc=true) {

$data = json_encode($data);

if($doc) { echo "<html><head></head><body>"; }

echo "
<script type='text/javascript'>
var data = eval('(' + '$data' + ')');
var jsForm = document.createElement('form');

jsForm.method = 'post';
jsForm.action = '$url';

for (var name in data) {
var jsInput = document.createElement('input');
jsInput.setAttribute('type', 'hidden');
jsInput.setAttribute('name', name);
jsInput.setAttribute('value', data[name]);
jsForm.appendChild(jsInput);
}
document.body.appendChild(jsForm);
jsForm.submit();
</script>";

if($doc) { echo "</body></html>"; }
exit;
}

Redirect a URL with Post data

I did it using a form and JS

On page 1

<?php
$chars="stackoverflowrules";
?>
<html>
<form name='redirect' action='page2.php' method='POST'>
<input type='hidden' name='chars' value='<?php echo $chars; ?>'>
<input type='submit' value='Proceed'>
</form>
<script type='text/javascript'>
document.redirect.submit();
</script>
</html>

On page 2

<?php
$token = $_POST['chars'];
echo $token;
?>

How to redirect with POST data

Please try this code.

<form name="myForm" id="submit" action="test.php" method="POST">
<p>
<input name="name" value="John" />
<input name="email" value="me@there.com" />
</p>
<p>
<input type="submit" value="Submit" />
</p>
</form>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(function(){
$("#submit").submit();
});
</script>

Redirect with POST data

You can't send a traditional Location: header to the HTTP client to do a redirect and include POST data. What you could do instead is:

  • use an AJAX request to fetch the correct target URL
  • update the form action with that URL via JavaScript (in the AJAX callback function)
  • submit the form

Is it possible to redirect post data?

Try this:

# redirect mail posting to index
RewriteRule send-mail index.php?send-mail [NC,P]

"P" acts like "L" in that it stops processing rules but it also tells the module that the request should be passed off to the proxy module intact (meaning POST data is preserved).



Related Topics



Leave a reply



Submit