How to Keep All the Post Information While Redirecting in PHP

How to keep all the POST information while redirecting in PHP?

if u want to carry forward your POST data to another pages ( except the action page) then use

session_start();
$_SESSION['post_data'] = $_POST;

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.

keeping php values in the page after redirecting

You have several methods to do this

  • Keep the original string and md5 hash in a session variable. It will persist even after redirects.

  • Instead of using the process.php, use the index.php as the target for your form. You can check the $_POST superglobal to see if you are loading the page for the first time, or displaying previously sent values.

  • Put the values passed to process.php in the query string of the redirection url, so instead of

    header("Location: index.php");

you do

header("Location: index.php?string=$mystring&md5=$mymd5");

Be careful though, you need to sanitize $mystring or your form would be an easy XSS bait. Thanks @machineaddict.

  • Since you are generating the md5 hash on the front, use js and ajax to send values to process.php without ever leaving the index. This one is the most elegant if you know your way around simple js libraries like jQuery.

PD: c'mon S.O., lists break code highlighting?

Redirect POST request and keep data. Possible?

If you really want to load balance through the code while potentially caching the page with the upload form, first select the default download server (url); then, onSubmit call the server and find the best upload target and adjust the action attribute accordingly.

With this method, users who do not activate JS still get what they want, users with JS enabled get the better upload target, and you can still cache. Additionally, the timing of the cache request could potentially be more opportunistic, since the URL request will occur very shortly before the actual upload.

The only hitch will be the call to get the URL, which you can more easily performance tune (I imagine) than the process you are describing above. Uploading a file twice through a header directive and/or cURL call don't seem like a good tradeoff for caching a single html file, IMO. But I don't know what you're up against, either.

If you don't want to heavily administer the server environment and introduce load balancing, this is the option I would suggest.

Note, also, I am not a server administrator by trade.

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.

How to keep POST method's input values with post/redirect/get?

You need to transport those values via GET.

There are two options to do that:

  1. sessions
  2. URL query parameters

If you simply save the data in the session, you can read it from there and re-populate the form. If you put it into the URL, you can read it from there; but obviously the URL will contain a lot of data then.

Reading it from a session the data will not be unique to the specific page/redirect, but anyway you open that page again within the same session will show the same data. Passing the data via the URL will make it unique to the specific request.

As a middle ground, you can save the data in the session tied to a specific random id, redirect with this id in the URL (e.g. example.com/foo.php?i=12345), the re-populate the data from the session with the specific id.

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!

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