Page 1 of 1

redirect using post

Posted: Thu May 26, 2005 11:09 am
by hessodreamy
The names here have been changed to protect the innocent. And make it easier to understand.

I have a payment page in domain A, which passes info via a POST to payment handling company B. The format of B's site means that info must come via POST, and must come from domain A.

Now I'm setting up another site on domain C, so I need to send the payment info to domain A, and redirect it (via post) to C.

But I'm having trouble redirecting info using post. How do I do it?

Posted: Thu May 26, 2005 11:11 am
by Burrito
I'm pretty sure you can set the headers to think it was a post, I'll research and get back to you on that...

the other alternative would be to use a form and submit it on body load:

ex:

Code: Select all

<html>
<body onLoad=&quote;document.MyForm.submit()&quote;>
<form name=&quote;MyForm&quote; method=&quote;post&quote; action=&quote;c.php&quote;>
<input type=&quote;hidden&quote; name=&quote;myVar&quote; value=&quote;your value&quote;>
</form>
</body>
</html>

Posted: Thu May 26, 2005 11:12 am
by hessodreamy
had thought about javascript, but that would be reliant on the browser.
I'll look into headers...

Posted: Thu May 26, 2005 11:44 pm
by harrisonad
do you mean:

Code: Select all

A send to B back to A send to C
and all info must be the same?

If it is, post the code in A telling that received infos from B for me to understand it better.

Posted: Fri May 27, 2005 4:42 am
by hessodreamy
No, I mean:
B posts to A, which redirects to C.
Hence C recieves B's info from A

Posted: Fri May 27, 2005 5:00 am
by harrisonad
Using just header("location: ".$url) to redirect will loose all post variables received by A.
Instead doing that, gather all these variables in A script and then resubmit it to C script using hidden fields.

Code: Select all

// receive
$name = $_POST['name'];
$age = $_POST['age'];
// build resubmission form
echo "
<form name=thisfrm method=post action='script_c.php'>
<input type=hidden name=name value='$name'>
<input type=hidden name=age value=$age>
</form>";
// resubmit
echo "
<script language='javascript'>
document.thisfrm.submit();
</script>";
Then the C script will receive all these infos as if it came from B.

If you have any questions about that, just lemme know. ok?

Posted: Fri May 27, 2005 5:02 am
by hessodreamy
That would do the trick, but only if the client has javascript enabled. I need something a bit more foolproof.

Posted: Fri May 27, 2005 10:17 am
by pickle

Posted: Fri May 27, 2005 10:44 am
by JAM
hessodreamy wrote:That would do the trick, but only if the client has javascript enabled. I need something a bit more foolproof.
There is also cURL that might be of interest.

Posted: Wed Jun 01, 2005 1:31 pm
by hessodreamy
I tried the code from codewalkers, but it didnt seem to do anything. Here's the code used:

Code: Select all

// Site information
$site = "localhost";
$url = "/print.php";

// Build your POST query
$req = "user=USERNAME&pass=PASSWORD";

// Header info to connect to the server
$header .= "POST $url HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= 'Content-Length: ' . strlen($req) . "\r\n\r\n";

// Open up a connection on port 80 to server
$fp = fsockopen ($site, 80, $errno, $errstr, 30);

// Can't connect? Then DIE!
if (!$fp)
die("I couldn't connect to $site");

// send POST request
fputs ($fp, $header . $req);

// Receive POST request
while ( !feof($fp) )
$response = @fread($fp,1024);

// $response = entire page
print $response;

echo "end script";
All I get is the 'end script'

I've tried cURL but apparently that just downloads a page, and does not pass control to it, which is what I need.

I get a similar blank page from a lot of scripts found. Am I doing something fundamentally wrong?