I am Confused About PHP Post/Redirect/Get

I am confused about PHP Post/Redirect/Get

However, couldn't a user just navigate to the URL above?

Yes, he can. This will not cause anything bad though.

Also, what is the purpose of the GET variable?

To have some flag that represents the fact that the form has been processed successfully and you need to congratulate user.

Couldn't I just have a script at form.php that checks if the user is logged in?

Uhm, you can keep your code in the way you like. There is no any strong requirements

At submit.php, should I save the logged in username as $_SESSION['username'], and then check if isset() at form.php?

If you need to persist it across the current session - yes, do so.

Also, since a URL with "success" in it isn't really pretty, is it economical to redirect the user once again?

Redirect where. Redirection is pretty cheap thing.

Should I use PHP header() or Javascript window.location.href?

You definitely should do that in php, otherwise you'll get the troubles you're trying to avoid following PRG-way.

Post/Redirect/Get on same page with firefox

You can use PHP to redirect. For example:

if (isset($_POST)) {
// processing the data
// ....

header('LOCATION: ' . $_SERVER['REQUEST_URI']); // <-- for dynamic URL
exit();
}

Using Post/Redirect/Get with data before the redirect... meta-refresh?

im not sure what is the right way to do this

but usually i use js redirect echo '<script>window.location="somefile.php";</script>';
to avoid this error

POST in PHP AND redirect the user to that page?

You cannot redirect POST requests. As simple as that. Any redirect will always turn into a GET request.

If you want to receive POST data, then send that data to another page, you have two choices:

  • if both pages are on the same server, use sessions to save the data server-side, don't make the client carry it over
  • if the destination is on another server and you need to send the client there together with the data, set up another intermediate form like you are

does post-redirect-get need to happen for an ajax request?

For something that only uses AJAX, I can't see a reason to use prg. Since it is not a user controlled action with the possibility of duplication, the only way the AJAX call would be duplicated is if the original page was refreshed before the action finished, and since prg has that same one flaw, you may as well use the direct approach.



Related Topics



Leave a reply



Submit