11

I am trying to forward data from one page to another without using cURL, is this possible?

At the moment i have tried

header('HTTP/1.1 307 Temporary Redirect'); header('Location: new-location.php'); 

This works nicely but gives a stupid pop up box, any other methods?

I have tried using curl but nothing happens, not sure if its enabled on my server!

13
  • 2
    Forward data? The header calls you posted simply do a redirect, and do not use cURL. Am I missing something? Commented May 30, 2011 at 19:34
  • 1
    @stefgosselin: The word "without", apparently. Commented May 30, 2011 at 19:34
  • Yes i know they do a simple redirect, but it carries the data with it. only problem is it gives a stupid popup box alerting the user Commented May 30, 2011 at 19:36
  • @Lunar: By what metric is it "stupid"? Commented May 30, 2011 at 19:38
  • 1
    @Lunar - Look at the link from netcoder. Your choices are realistically storing it in a SESSION variable or appending the POST data to the URL and reading it as a GET string. Commented May 30, 2011 at 19:59

5 Answers 5

14

I think following is the only possible way to achieve that, instead of redirecting with location header, send this html:-

<!DOCTYPE html> <html> <body onload="document.forms[0].submit()"> <form action="new-location.php" method="post"> <?php foreach( $_POST as $key => $val ): ?> <input type="hidden" name="<?= htmlspecialchars($key, ENT_COMPAT, 'UTF-8') ?>" value="<?= htmlspecialchars($val, ENT_COMPAT, 'UTF-8') ?>"> <?php endforeach; ?> </form> </body> </html> 
Sign up to request clarification or add additional context in comments.

3 Comments

Javascript is not very robust for form submits, better using server-side scripting for this.
@stefgosselin: so what do you suggest to write in PHP in order to forward the POST data?
@MarcoDemaio - See my answer below. Although my answer was downvoted, I tend to favor server-side processing for redirects.
14

You could use the following to forward post data to another url. Notice that the response is captured in the $data variable, with which you can do whatever you want.

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.domain.tld/urltopostto.php"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 100); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST)); $data = curl_exec($ch); curl_close($ch); 

1 Comment

Could you just put return $data; and effectively wrap the POST call?
2

If I understand the problem correctly and you're working on an Apache server with mod_rewrite enabled, you could use URL rewriting. You could set the request method as a condition and rewrite the original URL to the alternate one:

RewriteCond %{REQUEST_METHOD} ^POST RewriteRule ^index.php new-location.php [L] 

Comments

1

Using cURL or fopen() are common suggestions, but neither actually "forwards" the user to the other page.

To truly forward the user to another page with $_POST data intact, you can create an intermediate page dynamically that contains a form with the post data and JS code that submits the form immediately (along with a button or link to submit the form if the user does not have JS enabled).

You then forward the user to that intermediate page, and it forwards the user to the target page with the #_POST data intact. If the intermediate page has no visible content, the user won't know it's there.

The target page could unlink the intermediate page so there's no chance of a re-submission.

Comments

-3

Maybe, just maybe you can try it this way:

header("Status: 200"); header("Location: new-location.php"); exit; 

6 Comments

A header call will not send data, unless you set the variable at the end of your url, a la GET .. header("Location: new-location.php?var1=$var1&var2=$var2"); Ugly but it would work. You could also use sessions.
Then you haven't answered the question. :)
You have 2 choices from what I can gather, javascript as posted above (I don't recommend) or sessions.
"A header call will not send data, unless you set the variable at the end of your url, a la GET .. header("Location: new-location.php?var1=$var1&var2=$var2"); Ugly but it would work. You could also use sessions. " this worked for me in the end thanks
@Lunar - If this is what worked for you, maybe set the answer as the favored answer?. The selected solution currently relies on javascript and though it works seems lest robust than a 100% server-side redirect.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.