Jump to content

Help with error handling - having a PHP script call itself


weemikey

Recommended Posts

Hi all! I'm building a php/mysql system and I'm using an example of SESSION that I found here to check for a logged in user on each page. This includes a line that says "header("Cache-control: private");" after session_start(). I've used cookies before, but the SESSION seemed like a better idea.

 

My code builds a simple html form with a submit button. To do this I have code that says if we're not posting show the form  and submit button. The "if we're posting" block then validates the data and makes a new db record if all is well.

 

So in my "if we're posting" block (meaning $_POST is true) I check for values in the fields and if I find an error I make an error message to be passed back and displayed.

 

What I've BEEN doing in my other little projects is using a line like this, which is SUPPOSED to run the script again, but display an error above the form to show the user what went wrong. However, I'm getting the "Cannot modify header information" error because I already use "header" for the session.

 

// RETURN TO THE PAGE WITH AN ERROR MSG
$field_err_msg = "all fields must contain values";
header("add_memberlist.php?field_err_msg=".$field_err_msg."&theme_id=".$theme_id."");
exit;

 

So, how can I have the script run itself again without using the header statement? I'm learning as I go, so I might be doing this ass-backward!

 

Thanks a million for any ideas!

Mikey

http://www.phpfreaks.com/forums/index.php/topic,37442.0.html

 

You can use output buffering to control header output, although that seems like a strong solution for this problem.  What I usually use is flow control (if or select statements).  Since you are printing the form in this script anyway, why not just rearrange your code logic to print an error and return to the same point where you'd be "not POSTing", that is, print the form again?  Also, are you redirecting on a successful POST?  You probably should be since a second record will be created if the page is reloaded.

 

And I think your header() statement should be:

 

header("Location: add_memberlist.php?field_err_msg=".$field_err_msg."&theme_id=".$theme_id."");

 

Incidentally, I've used that line after a session_start() without any difficulty.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.