PHP - Redirect and send data via POST PHP - Redirect and send data via POST php php

PHP - Redirect and send data via POST


You can't do this using PHP.

As others have said, you could use cURL - but then the PHP code becomes the client rather than the browser.

If you must use POST, then the only way to do it would be to generate the populated form using PHP and use the window.onload hook to call javascript to submit the form.

C.


here is the workaround sample.

function redirect_post($url, array $data){    ?>    <html xmlns="http://www.w3.org/1999/xhtml">    <head>        <script type="text/javascript">            function closethisasap() {                document.forms["redirectpost"].submit();            }        </script>    </head>    <body onload="closethisasap();">    <form name="redirectpost" method="post" action="<? echo $url; ?>">        <?php        if ( !is_null($data) ) {            foreach ($data as $k => $v) {                echo '<input type="hidden" name="' . $k . '" value="' . $v . '"> ';            }        }        ?>    </form>    </body>    </html>    <?php    exit;}


Another solution if you would like to avoid a curl call and have the browser redirect like normal and mimic a POST call:

save the post and do a temporary redirect:

function post_redirect($url) {    $_SESSION['post_data'] = $_POST;    header('Location: ' . $url);}

Then always check for the session variable post_data:

if (isset($_SESSION['post_data'])) {    $_POST = $_SESSION['post_data'];    $_SERVER['REQUEST_METHOD'] = 'POST';    unset($_SESSION['post_data']);}

There will be some missing components such as the apache_request_headers() will not show a POST Content header, etc..