Is it possible to send POST data using PHP redirect?

Update: this is NOT a duplicate. How to send a POST request with PHP? . The solutions there do not work for me, they simply display the result of the request, I do not want to do this, I want to send the user to an external site, this is a safe website for processing credit card payments.

Update 2: I added the chart below to try to clearly explain what I'm trying to do

Here is what I am trying to do:

  • html form passed to php script

  • The PHP script does some data processing and then sends the data through a POST request to an external website.

  • I do not want to receive the result of the POST request in my PHP script, instead I just want to send the user to an external site where they see the POST result

I think curl is not suitable for this task, since I'm not interested in getting the result of the request back, I just want to send the user to the next site in the same way as if they sent the form directly.

To add this in another way, I want to send the form to an external website, but first I want to send it to my own script so that I can process the data and send email notifications, and then send them to the external website with the addition of new ones calculated data.

One way I can think of is to output the new form output as hidden fields along with javascript to automatically submit the form, but there should be a simpler, more reliable way to do this without relying on javascript. Maybe by manipulating the headers or maybe there is already a php function to just send a send request and redirect the result?

What am i trying to do

+6
source share
9 answers

In short: no, this is not possible with PHP .

One way I can think of doing this is to output the new form output as hidden fields along with javascript to automatically submit the form ...

This is the only way to do this.


The question is a possible duplicate of PHP Redirects with POST data .


As a suicide , you can use HTTP 307 - Temporary Redirection .
From the HTTP / 1.1 documentation above:

If the status code 307 is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request if it cannot be confirmed by the user , as this may change the conditions under which the request was issued.

My brave one simply emphasizes that redirecting without user confirmation actually depends on the implementation of the browser protocol.

Redirects can be implemented in a PHP script that receives a POST request as follows & ast ;:

header('HTTP/1.1 307 Temporary Redirect'); header('Location: <your page in an external site>'); 

Main use Example:

page1.html - page containing 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 that processes your POST data before redirecting

 <?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 should be redirected (i.e. an 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'; 

& AST; don't try this at home!

+6
source

It seems to me that you are trying to redirect from one page to another by sending POST data. I am afraid this is not possible with PHP.

The simplest workaround I can come up with is to use cURL to (1) send your $_POST data to another server, (2) get the result from that server, and then (3) make a simple redirect to another page. Although this requires only 3 HTTP requests (a bit for such a simple problem), it more or less achieves what you are trying to achieve.


Let me break this approach down to the three steps mentioned in your question:

For step 1, any form of HTML will do if it has a method attribute with a value of post. If you want to submit the form without using JavaScript, just make sure that it has a submit button. This will send your data to the page defined in the action attribute of your form when you click the submit button:

HTML example:

 <form action="action_page.php" method="post"> First name:<br /> <input type="text" name="firstname" value="Mickey"><br /> Last name:<br /> <input type="text" name="lastname" value="Mouse" /><br /><br /> <input type="submit" value="Submit" /> </form> 

For step 2, you need to create a page with a name identical to the value of the action attribute of your form. In our example, this will be action_page.php . Here you check if $_POST any values ​​for the form fields that you submitted. In the case or our example, you can do something like this:

 if (isset($_POST['firstname']) && isset($_POST['lastname'])) { // Do the stuff you want to do on your own server here! } else { // Do nothing here! } 

For step 3, you need to make a second POST request using the $_POST data you just received from your HTML form. The easiest way to do this is to use cURL. Then you redirect the user to the second page, depending on the result of your second POST request.

To improve readability of the sample code, I put all the code associated with the cURL request into a function:

 function forwardRequest($url, $data) { $result = [ 'status' => NULL, 'last_url' => NULL, 'response' => NULL ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $result['response'] = curl_exec($ch); $result['status'] = curl_getinfo($ch, CURLINFO_HTTP_CODE); $result['last_url'] = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); curl_close ($ch); return $result; } if (isset($_POST['firstname']) && isset($_POST['lastname'])) { // Do the stuff you want to do on your own server here! $result = forwardRequest("http://www.example.com/processdata.php", $_POST); if ($result['status'] === 200) { header('Location: http://www.example.com/ok.php'); } else { header('Location: http://www.example.com/error.php'); } } else { header('Location: http://www.example.com/youshouldnotbehere.php'); } 

Note 1:

I am afraid that there is no general way to determine if everything is going well in an external system when data is sent to that system.

In my sample code, I use the status code of the HTTP response received by your cURL request. This solution only works when the server returns a status code that is not 200, if something is wrong on this server. You will need to run a few tests to make sure this approach is enough for your use.

If you cannot use the status code to determine if everything went well or not, consider checking the last effective URL or searching for keys in the response header and response text.


Note 2:

Instead of forwarding your user after processing the cURL request, you may just need to show the response text from the server to your user. If this result contains a lot of unwanted information that you do not need, consider parsing the result using DOMDocument::loadHTML or a third-party library (for example, Masterminds / html5 -php or PHPPowertools / DOM-Query ).

You can also simply write your own response text.

+7
source

Short answer:

  • POST to an external server
  • Parsing Analysis
  • hear nothing
  • use the header ('Location: url? param = res');
  • exit; // This is an important view

I do not want to receive the result of the POST request in my PHP script, instead I just want to send the user to an external site where they see the POST result

If you send a POST request with a PHP script, then the response will ALWAYS return to the same PHP script. I think that you want NOT to send this response to the user, but to redirect the user to another URL. What are you sure you are doing for?

 header('Location: url?param=res') ; exit ; 

and sends nothing to the client before calling header ()

+3
source

you can only submit values ​​while submitting the form

 action = "path/filename.php"; 

You can post form values ​​on this form.

if you want to redirect the file at the moment when you can pass values ​​via URL

 header('Location: url/filename.php?param1= '); 

try this for the local values ​​of the file you want to publish.

first you need to assign values ​​from the form, and then move the header with the variables. this may be the right decision on my part. thanks.

+3
source

I have one solution for you that will work in 99% of cases or in 100% of cases depending on how you implement it. All you have to do is process the form using AJAX. I know you need a free Javascript way, but this is the easiest way. If you use the jQuery library, then the SO documentation and answers are so simple for you that you don't need to write a lot of code. Just change some. Here is the concept:

1) The page loads as usual with the action of the forms pointing to the payment processing site. Since you are using Javascript (jQuery) to submit the form, you would replace the html submit buttons as follows:

 <input type="button" value="Check Out" onclick="NameofAjaxFunctionHere();" 

2) Change the meaning of the action of the forms. Before the actual AJAX code that submits the form behind the scenes to be processed, use something similar to this code to change the action to your own PHP page:

  $('#formIdHere').attr('action', 'yourPHPpageAndPathHere.php'); 

This code is intended for jQuery, but if necessary it is easy to change it to plain JavaScript. Now also make sure your AJAX code does not have a reset form. Many copies and past codes kindly do this for you.

3) (Optional) At this point, the form was submitted to your PHP page, and you did what you need with it. You now have two options:

  • Do not wait for an answer and just go to number 4.
  • Wait for a response and respond accordingly + change data if necessary

If you need to make sure that the data is correct or you need to change some of them, do it now. If the form data does not work, your process (for example, changed data) sends FALSE and goes to number 4. If you need to change what is a simple process. Send back all the data that needs updating in the array, and scroll through it in the same AJAX code that started this process. With Javascript, you can change the values ​​of the forms.

 [Code missing because I think you can figure this out if you need to do that. If not comment and when I have time I will post an example] 

4) Change the action of the forms. Your AJAX processing is complete. Traditionally, it erases the form and displays the message. If you select the option from above that will send FALSE to stop sending if there is bad data, just return false from the AJAX script now (maybe show something before this return, saying that the form has not been submitted).

  $('#formIdHere').attr('action', 'paymentUrlHere'); 

Now that the payment URL has returned to the place, and all the data has been updated, if you need to go this route, send the form as usual (or will). IN jQuery it could be simple:

  $('#formIdHere').submit(); 

And I think it covers. At this point, the user got out of your hands and was sent to the website to process payments with the usual POST. You even had a chance to change the data from the form, if necessary.

OH PS . Here is what I had in mind at 99% and 100% at the beginning of this post. 100% is an example that I gave you. No one can submit a form without first contacting their PHP page. The 99% solution would be to normally send the html on the page like this:

 <input type="submit" ...> 

Then, using Javascript, stop the default submit action and follow my steps 1-4. That way, if someone disables javascript (or is on the device that blocks it), the form still sends the payment processor at least. It’s up to you what you want to do.

+3
source

It is impossible, beacuase the page is fully loaded, there is a chance with empty data with a POST request, instead of using this session creation when it is required, and after using this session, you simply destroy the session. And the second is the use of CURL.

+1
source

You have a great plan in the description. Why don't you just generate a new 3-page POST request with the edited data on your page 2 after formatting? You can test the new POST request with the Chrome Postman extension

+1
source

he cannot use php.

But you can use this setting. In http://example.com/script.php you can write javascript auto submit code with checksum. For instance.

 <body onload="document.frm1.submit()"> <form method="POST" action="https://gatewaysite.com" name="frm1"> <input name="checksum" value="yourchecksumvalue" /> <input name="variable2" /> </form> </body> 
+1
source

If you own both websites, you can simply include the sites in each .htacess file, which will remain encypted.

The qualification for this is SSL CA on both sites.

in your .htaccess, you do this: this is added to htaccess site # 1:

  RewriteCond %{HTTP_REFERER} !^https://www.site2.com/.*$ [NC] RewriteCond %{HTTP_REFERER} !^https://www.site2.com$ [NC] 

this is added to htaccess site # 2:

  RewriteCond %{HTTP_REFERER} !^https://www.site1.com/.*$ [NC] RewriteCond %{HTTP_REFERER} !^https://www.site1.com$ [NC] 

Then you can use the standard html form on site # 1, for example:

  <form action="https://wwww.site2.com" method="POST"> 
0
source

All Articles