Inconsistant Order of Execution...?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jrucifer
Forum Commoner
Posts: 32
Joined: Fri May 13, 2005 2:36 pm
Location: phoenix

Inconsistant Order of Execution...?

Post by jrucifer »

This isn't making sense to me at all... I'm trying to use session variables to display an error message, and then unset the variable.. The method works fine on 2 different login pages, but I'm trying to use the same procedure on a different form and it's unsetting the variable before the echo...

Here's my code:

processing page... ($msg is set on input length errors)

Code: Select all

if($msg != "") {
			$_SESSION["inputs"]=$inputs;
			$_SESSION["msg"]=$msg;
			header("location:/signup/");
			}
and then back to sign up page...

Code: Select all

<?
				if(isset($_SESSION["msg"])) {
				
				echo "<div class=\"error\">".$_SESSION["msg"]."</div>";
				unset($_SESSION["msg"]);
				}
?>
It's so simple that I can't understand why it's not working... and only on this page, the same code works fine elsewhere. If I remove the 'unset' line, it displays the message as expected. I've even tried assigning $_SESSION["msg"] to a new variable at the top of the page, and unsetting $_SESSION["msg"] at the bottom of the page. What the hell is going on?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

var_dump($_SESSION['msg']);
if(isset($_SESSION["msg"])) {
   echo "<div class=\"error\">".$_SESSION["msg"]."</div>";
   unset($_SESSION["msg"]);
}
What does this display on the problematic page?
jrucifer
Forum Commoner
Posts: 32
Joined: Fri May 13, 2005 2:36 pm
Location: phoenix

Post by jrucifer »

before submitting the form, a PHP notice telling me theres no index for "msg", same thing after submitting the form... Why is it unsetting the session variable before executing any other code?? again, if I remove the 'unset' line, var_dump prints out the session variable info after the form submission...
jrucifer
Forum Commoner
Posts: 32
Joined: Fri May 13, 2005 2:36 pm
Location: phoenix

Post by jrucifer »

adding "exit;" after the header redirect solved it... I thought php scripts "exit" automatically at the end of the script?

edit: duh.. it's cause I'm running the processing script on the same page as the form, using if($_SERVER["REQUEST_METHOD"] == "POST")... The script was continuing, unsetting the session variable, and then redirecting to the same page... silly me.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Also remember that if you are redirecting that it is all in the same request, so you may need to call session_write_close() on the first page for the second page to be able to access them.
(#10850)
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

arborint wrote:Also remember that if you are redirecting that it is all in the same request, so you may need to call session_write_close() on the first page for the second page to be able to access them.
I guess this is only relevant when using frames
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

jmut wrote:I guess this is only relevant when using frames
It can be a problem with frames because of race conditions or redirects because they are in a single request.
(#10850)
Post Reply