redirect using post

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hessodreamy
Forum Commoner
Posts: 58
Joined: Wed Apr 20, 2005 8:11 am

redirect using post

Post 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?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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>
hessodreamy
Forum Commoner
Posts: 58
Joined: Wed Apr 20, 2005 8:11 am

Post by hessodreamy »

had thought about javascript, but that would be reliant on the browser.
I'll look into headers...
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post 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.
hessodreamy
Forum Commoner
Posts: 58
Joined: Wed Apr 20, 2005 8:11 am

Post by hessodreamy »

No, I mean:
B posts to A, which redirects to C.
Hence C recieves B's info from A
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post 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?
Last edited by harrisonad on Fri May 27, 2005 5:16 am, edited 1 time in total.
hessodreamy
Forum Commoner
Posts: 58
Joined: Wed Apr 20, 2005 8:11 am

Post by hessodreamy »

That would do the trick, but only if the client has javascript enabled. I need something a bit more foolproof.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
hessodreamy
Forum Commoner
Posts: 58
Joined: Wed Apr 20, 2005 8:11 am

Post 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?
Post Reply