Welcome Guest, Not a member yet? Register   Sign In
Session data removed or overwritten
#1

(This post was last modified: 12-22-2015, 11:33 AM by Shawn.)

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
try {
    $this->donation->update($transaction_id, $program_id, $friend_id);
} catch (Exception $e) {
     log_message('error', $e->getMessage());
     //On caught error create a flash message
     $this->session->set_flashdata('message', 'Unable to update donation record');
     //Flash message not working try user defined message as well.
     $this->session->set_userdata('ud_message', 'UD Unable to update donation record');
     //Both of the following lines work (when uncommented;-), indicating that the catch block is entered.
     //echo print_r($_SESSION);
     //show_error('Unable to update the donation record.');
}
redirect('ledger');
}

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
       if (NULL !== $_SESSION['message']) {
            $view_content['message'] = $_SESSION['message'];
       }
       else {
           $view_content['message'] = 'no message';
       }
       //Checking for the existence of the flash message
       if (NULL !== $this->session->userdata('ud_message') ) {
            $view_content['ud_message'] = $this->session->userdata('ud_message');
       }
       else {
           $view_content['ud_message'] = 'no message';
       }
       
       //Prints what is in the session. Identity information is still here but the message and ud_message are not
       echo print_r($_SESSION);
       
      $this->data['main_content_view'] = $this->load->view('share/ledger_view', $view_content, TRUE);
      $this->load->view('share/default', $this->data);

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
Reply
#2

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';
}
```
Reply
#3

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
        $message = $_SESSION['message'];
        $view_content['message'] = $message;

        //Checking for the existence of the user defined data
        if (NULL !== $_SESSION['ud_message']) {
            $view_content['ud_message'] = $this->session->userdata('ud_message');
        }
        else {
            $view_content['ud_message'] = 'no message';
        }
Also the non-flash item (ud_message) is also getting over-written somehow. It makes me wonder if the Ion Auth library over-writes the session.
Reply
#4

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.
Reply
#5

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.
Reply
#6

(01-03-2016, 03:38 PM)Shawn Wrote: 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.

... 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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB