PHP open another web page with POST data

I am new to PHP and I am trying to do something that may be bad practice and may be impossible. I basically just hack something together to test my knowledge and see what PHP can do.

I have one webpage with a form that collects data. This is sent to a PHP script that does a bunch of processing, but doesn't actually display anything important. I want that after the processing is completed, the script then tells the browser to open another page on which the results are displayed.

I know that I can use the header ('Location: page.php'); but I can't decide how to provide POST data with this. How can i do this? Also, is there any other way to tell the browser to open another page?

EDIT: what I take from the answers is that this can be done using various hacks, but I would be better off just processing and displaying the code in a single file. I am pleased with this; it was an experiment more than anything.

+6
redirect post php header
source share
8 answers

Is it really necessary to call another page after processing is complete? I would do the following:

<form method="post" action="display.php"> ... </form> 

display.php:

 if ($_POST) { require_once(process.php); process($_POST); display_results; } 

with process.php containing the code needed to process the mail request.

Alternatively, you can use something like the cURL library to transfer processing results to the page you specify. I don’t know if this is really what you need.

+4
source share

You can save this data in a session, for example. in the first file that processes the message

 session_start(); $_SESSION['formdata'] = $_POST; //or whatever 

then you can read it on the next page, for example

 session_start(); print_r($_SESSION['formdata']); 

or you can pass it via GET: (but commenting is a bad idea)

 header('Location: page.php?' . http_build_query($_POST)); 

If you do this, make sure that you do additional processing / validation on the .php page, as a malicious user can modify the variables. also, you may not need all the mail sent to the next page

Edit

I must clearly indicate that, in my opinion, the second option is worse, since you are limited by the size of the data that you can send via get, and perhaps less secure, since users can manipulate the data more explicitly.

+16
source share

You can use JavaScript as dirty work:

 <form id="redirect_form" method="post" action="http://someserver.com/somepage.php"> <input type="hidden" name="field_1" value="<?php echo htmlentities($value_1); ?>"> <input type="hidden" name="field_2" value="<?php echo htmlentities($value_2); ?>"> <input type="hidden" name="field_3" value="<?php echo htmlentities($value_3); ?>"> </form> <script type="text/javascript"> document.getElementById('redirect_form').submit(); </script> 

(the script should be below the form)

+2
source share

It is not possible to redirect the user's browser to an arbitrary page and send a POST request. This will be a small security risk when any link can force you to submit any form to an arbitrary site without any clue about what will happen.

In short, this is not possible

0
source share

AFAIK, this is usually done as a two-step process:

  • In form.php, send the data to script process.php
  • Process.php script processes the data, but never outputs anything itself, it always calls the header ("Location: asdasd") to redirect to the success.php or failure.php page (if applicable)
0
source share

Do it all in one script and just output another HTML for the results.

 <?php if($doingForm) { ?> html for form here <?php } else { ?> html for results <? } ?> 
0
source share

This problem annoyed me several times. My custom CMS does rather complicated processing, loading and manipulation, so sometimes it gives out rather long error messages and information that are not suitable for conversion to GET data, and I always wanted to avoid the problem of reloading INSERT data, but have not yet found an adequate solution.

I believe that the right way to do this is to create message arrays for each possible state - each message or error that you could display, and then you only need to send error / message numbers, which are much simpler than long lines of data, but this is that I always avoided personally, because it seems to me that this is a little tiring and cumbersome. Honestly, this is probably just laziness on my part.

I really like the solution for storing the SESSION variable, but the question is how to ensure proper destruction of SESSION data? As long as you guarantee that you are only sending information (messages / errors) and not data that should / could have been stored (and therefore potentially sensitive), this should be a problem that can be avoided.

0
source share

I hope I understood your question correctly. You can try the following:

  • Adjust the shape like this:

form method = "POST" action = "process_data.php"

2. Then you create a process_data.php file that unexpectedly processes the data.
And in this file you use the header:
For example:

$ head = sprintf ("page.php? data1 =% d? data2 =% d", $ data1, $ data2);
heading ($ head);

I hope I can help.

-3
source share

All Articles