PHP redirection, save POST

I have an html page with a checkbox shape. The form has its own action pointing to a PHP script. The PHP script collects the POST variables just fine, but obviously a blank screen is displayed because it runs on www.example / script.php after execution.

How can I get PHP to go to another URL to get more information about the form submission while saving these POST files?

header() and metadata seem to cancel everything and not collect data ... How do I collect this data in POST and then automatically go to another html page for another form with a PHP script application as its action?

Thank you and sorry if I formulated this in confusion.

+6
source share
2 answers

You can either save the $_POST variables to $_SESSION and then send them when the final part of the form is complete, or you could have an intermediate page to store these values ​​as hidden inputs and send them to the last page.

+5
source

I found that this code works almost all the time (except when you want to forward using personalized mail data and the client does not support javascript).

This is done by abusing the 307 Temporary Redirect , which seems to be sending POST data or by creating a self-submitting javascript form.

This is a hack, but use it only if you SHOULD forward POST data.

 <?php function redirectNowWithPost( $url, array $post_array = NULL ) { if( is_null( $post_array ) ) { //we want to forward our $_POST fields header( "Location: $url", TRUE, 307 ); } elseif( ! $post_array ) { //we don't have any fields to forward header( "Location: $url", TRUE ); } else { //we have some to forward let fake a custom post w/ javascript ?> <form action="<?php echo htmlspecialchars( $url ); ?>" method="post"> <script type="text/javascript"> //this is a hack so that the submit function doesn't get overridden by a field called "submit" document.forms[0].___submit___ = document.forms[0].submit; </script> <?php print createHiddenFields( $post_array ); ?> </form> <script type="text/javascript"> document.forms[0].___submit___(); </script> <?php } exit(); } function createHiddenFields( $value, $name = NULL ) { $output = ""; if( is_array( $value ) ) { foreach( $value as $key => $value ) { $output .= createHiddenFields( $value, is_null( $name ) ? $key : $name."[$key]" ); } } else { $output .= sprintf("<input type=\"hidden\" name=\"%s\" value=\"%s\" />", htmlspecialchars( stripslashes( $name ) ), htmlspecialchars( stripslashes( $value ) ) ); } return $output; } 
+2
source

Source: https://habr.com/ru/post/896530/


All Articles