How do I implement post/redirect/get in PHP if my handler requires session data?
I'm trying to understand how I would implement the post/redirect/get pattern if my handler page where everything is $_POST'd to requires session data (checking if the user is logged in, lets say)
I can't call: header("Location /somene开发者_Python百科wplace", 303);
because I'd get a 'Cannot modify headers' error, as I've already called session_start() to get the session data.
Can someone help me understand this pattern a bit better, should your handler require interacting with session data?
Thank you,
Usually when I get that error its because something has already been outputted to the user. You cannot output any data before calling header(). Check to see if you are printing/echoing anything before header is called. Also check your closing tags on your scripts, if you have a space after a closing tag ?> php will output that to the user and set the headers.
There is something wrong in your code. The session_start()
shouldn't send the headers. Use PHP output buffering to ensure no output is sent.
A call to session_start() should only modify the headers. You probably outputted some data somewhere else in your script. When PHP gives you the message 'Cannot modify headers' it usually tells you on which line the output was started.
So sessions should not prevent you from doing any redirects or other things.
Check out using ob_start() and ob_end_flush() to use output buffering.
精彩评论