Session data removed or overwritten |
I have set up an update to fail with a database error. I catch the error, log to file, and store an error message for display. Unfortunately, when I redirect to a summary page the messages disappear from my session. The summary page contains a variable to display any messages or '' if none.
Each controller inherits a controller that checks the authentication Code: if (!$this->ion_auth->logged_in()) Here is where I set the message in the session variables: message and ud_message Code: // This method call deliberately set to fail with db error Here is the code in the ledger controller where I check the session variable for error messages Code: //Checking for the existence of the flash message The identity (Ion Auth) values remain in the session but the other items I added in the error get removed or over-written. Here is the session printed inside the try catch immediately after setting the flash and userdata variables: Array ( [__ci_last_regenerate] => 1450750143 [identity] => [email protected] [username] => so and so [email] => [email protected] [user_id] => 2 [old_last_login] => 3333344444 [message] => Unable to update donation record [__ci_vars] => Array ( [message] => new ) [ud_message] => UD Unable to update donation record ) 1 Here is the the session printed out in the ledger controller Array ( [__ci_last_regenerate] => 1450750445 [identity] => [email protected] [username] => so and so [email] => [email protected] [user_id] => 2 [old_last_login] => 3333344444 ) 1 Any suggestions would be much appreciated
Those are "flashdata" session variables. That means that once accessed they are no longer available (they are deleted). You've already accessed them just before printing out the $_SESSION:
```php if (NULL !== $_SESSION['message']) { $view_content['message'] = $_SESSION['message']; } else { $view_content['message'] = 'no message'; } if (NULL !== $this->session->userdata('ud_message') ) { $view_content['ud_message'] = $this->session->userdata('ud_message'); } else { $view_content['ud_message'] = 'no message'; } ``` Website: http://avenir.ro
Good to know. It's still not working when I reference the flash variable just prior to using it:
Code: //Display flash messages in the view
Additional information:
I halted the program in the controller where I set the userdata and the flashdata I print_r the session and it shows both the flash data and the longer term userdata item + the Ion Auth identity data But when I select * from ci_sessions only the Ion Auth identity data is there. When does codeigniter save session data to the ci_sessions table? I would expect the userdata to be there, even if the flashdata gets removed.
This conundrum was resolved by the answer to a separate topic (thanks Narf):
http://forum.codeigniter.com/thread-6400...#pid327251 In brief: If you have a db error which results in a rollback then any session data that you set in that context will also be rolled back. The alternative, if you need to set session data, is to use manual transactions so that you can roll back explicitly and then set your session data. (01-03-2016, 03:38 PM)Shawn Wrote: This conundrum was resolved by the answer to a separate topic (thanks Narf): ... or use MyISAM for the sessions table, because it doesn't support transactions. Or use another session driver, like files. Seriously, unless your application directly accesses the sessions table, there's no advantage whatsoever to using the database driver. |
Welcome Guest, Not a member yet? Register Sign In |