Andrew G. Johnson
Andrew G. Johnson

Reputation: 27003

Possible to post data in a PHP redirect?

Well right now I have a page (page2.php) and at the top it does some validation checking and if something fails it bounces back to page1.php. The problem is that page1.php is loaded by a post meaning it is the end result of a form. This means that all the post data I originally have on page1.php is lost.

So here is my code:

if ($validation_fails)
{
    header('Location:page1.php');
}

Upvotes: 5

Views: 9425

Answers (4)

Zoredache
Zoredache

Reputation: 39663

On page2.php You could store the contents of the post into a $_SESSION variable if validation fails.

On page1.php you simply include a properly escaped/encoded version of the data that you stored in the session as part of the form. You would also be able to use this to update the form so it is clear the user what part failed the validation.

Upvotes: 1

Ólafur Waage
Ólafur Waage

Reputation: 70031

You could restructure the logic. The validation should happen on page1.php and if it fails, the redirection does not happen. If it succeeds, you are redirected.

If you are concerned about safety (people just going to page2), you could set a session variable that is checked on page2 and set on page1.

Upvotes: 3

Gumbo
Gumbo

Reputation: 655825

Can’t you just include the validation script via include or require instead of redirecting to it?

Upvotes: 2

Paolo Bergantino
Paolo Bergantino

Reputation: 488734

You can post data back with cURL or re-structure to do the validation at the top of page1.php and if it doesn't fail, take to page2.php. If you are doing some sort of multi-step form, you could save all the data in a session and populate fields if there's matching data in the session. Not sure if that applies, though.

Upvotes: 5

Related Questions