Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Redirect, Keep POSTs

Tags:

php

I have an html page with a checkbox form. The form has its action pointing to a PHP script. The PHP script collects the POST variables just fine but obviously a blank screen displays because it goes to www.example/script.php once executed.

How I do get PHP to go to another URL for more form submission information while keeping those POSTs intact?

header() and metaredirect seem to overrule everything and not collect the data... How do I collect that data into POSTs and then automatically go to another html page for another form with a PHP script attached as its action?

Thanks and sorry if I worded this in a confusing manner.

like image 941
Redirect42 Avatar asked Sep 03 '11 00:09

Redirect42


People also ask

How to automatically redirect PHP?

PHP 301 Redirect To set a permanent PHP redirect, you can use the status code 301. Because this code indicates an indefinite redirection, the browser automatically redirects the user using the old URL to the new page address.

How pass data redirect in PHP?

Redirection from one page to another in PHP is commonly achieved using the following two ways: Using Header Function in PHP: The header() function is an inbuilt function in PHP which is used to send the raw HTTP (Hyper Text Transfer Protocol) header to the client.

How redirect data from one page to another in PHP?

In PHP, when you want to redirect a user from one page to another page, you need to use the header() function. The header function allows you to send a raw HTTP location header, which performs the actual redirection as we discussed in the previous section.

What is data redirect?

Data redirection is when the input or output of a command is redirected using special notation. It is also used for opening and closing files for read and write operations. The forms and syntax of redirection are varied and their meanings are specific.


2 Answers

You could either store the $_POST variables in $_SESSION and then submit them when the final part of the form is completed, or you could have the intermediary page store these values as hidden inputs and submit them to the final page.

like image 170
Christopher Armstrong Avatar answered Sep 20 '22 17:09

Christopher Armstrong


I've found that this code works almost all the time (except in some cases where you want to forward using custom post data and the client doesn't support javascript).

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

This is a hack though, only use it if you MUST forward the 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's 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;
}
like image 24
Kendall Hopkins Avatar answered Sep 22 '22 17:09

Kendall Hopkins