Jump to content

Odd Session Behaviour


DooVoo

Recommended Posts

Hi All,I've been wrestling with this problem for two days now and just cannot work out what the cause is! Below is a simple piece of code that is designed to return a view (for now, just simple html) depending on one of 3 states of the system. This state is stored in a SESSION var and is manipulated by the GET parameter 'cmd' (i.e. cmd=Next etc). With these commands, additional information can also be sent, these are automatically stored into the SESSION under the appropriate name (i.e. cmd=Next&name=Paul) the $_SESSION['name'] would be set to 'Paul'. This works fine for the first input but when an email address is given in state 2, the data is not shown in state 3 with the name from stage 1 (if that makes sense :) ). However, the data is being stored as when you refresh the page and redisplay stage 3 the data appears!The code snippit is below:

<?session_start();/*        Registration Form Script*/// Includes//include("includes/mysql.inc.php");// Constantsdefine("MIN_STAGE_COUNT","1");define("MAX_STAGE_COUNT","3");// Functionsfunction PrevStage(){        if ($_SESSION['Stage'] > MIN_STAGE_COUNT)                $_SESSION['Stage']--;}function NextStage(){        if ($_SESSION['Stage'] < MAX_STAGE_COUNT)                $_SESSION['Stage']++;}// Beginprint("DEBUG: Begin<br>");print("DEBUG: Checking Session<br>");if(!isset($_SESSION['Stage'])){        print("DEBUG: Stage not set. Defaulting to 1<br>");        $_SESSION['Stage'] = 1;}else{        foreach ($_GET as $field => $value)        {            print("DEBUG: Switching Field " . $field  . ".<br>");            switch ($field)            {                case "cmd":                        switch ($value)                        {                                    case 'Next':                                        print("DEBUG: Command Next.<br>");                                        NextStage();                                        break;                                    case 'Prev':                                        print("DEBUG: Command Previous.<br>");                                        PrevStage();                                        break;                        }                        break;                default:                        print("DEBUG: Unknown Param " . $field . "=" . $value . " - Storing to session.<br>");                        $_SESSION[$field] = $value;                        break;            }        }}$Pages = array();$Pages[1] =     '<h2>Enter your name...</h2>                 <form name="Form1">                        <input type="textbox" name="name" id="name">                        <input type="button" value="Next Stage" onClick="javascript: NextStage();">                 </form>';$Pages[2] =     '<h2>Please enter your email...</h2>                 <form name="Form2">                        <input type="textbox" name="email" id="email">                        <input type="button" value="Previous Stage" onClick="javascript: PrevStage();">                        <input type="button" value="Next Stage" onClick="javascript: NextStage();">                 </form>';$Pages[3] =     '<h2>Welcome ' . $_SESSION['name'] . '!</h2>                 <p>You gave your email as: ' . $_SESSION['Email'] . '</p>';print($Pages[$_SESSION['Stage']]);print("DEBUG: Stage = " . $_SESSION['Stage'] . "<br>");print("DEBUG: End<br>");session_write_close();?>

I'm guessing it has something to do when the way I am setting the session values and that the returned data is being returned before the session has been updated. I just can't see it. Any help would be hugely appreciated!

Link to comment
Share on other sites

It seems the session data was being set after the pages array so the session data wasn't available until after a refresh. Moving the pages array initialisation solved the issue. The code above works lovely now!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...