Question

I have been doing this for a while now, but still get confused when it comes to PRG. I know how it works so for example I would do:

if($_POST){
 header("Location: ".$_SERVER['REQUEST_URI']);
 exit();

}

Because it stops user refreshing and sending the POST to the server again. But I keep getting the "Warning: Cannot modify header information - headers already sent" blah blah message. I know this is because the redirect needs to go before any whitespace or echo-ing or text etc... so right above the header would be ideal!

But if my form is sent to a random .php page with no header or anything (where I can process data and edit database etc), this makes more sense to me - but this way I can't include any of the Wordpress functionality to process my data and do what I need to do. Like I would include a few lines of Wordpress to allow me to access the functions, but this way I cant process the data and THEN redirect the user.

At the moment to get round this I am processing data and using a meta-refresh to redirect, and I know this is BAD!

Hope this makes sense :) Any help would be really appreciated!

Était-ce utile?

La solution

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

Autres conseils

I usually use location.replace(URL) so if the user hits back on their browser they are not trapped in a redirect maze:

window.location.replace(urlToRedirectTo);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top