Question

Description

I am trying to add a custom object called ReviewHolder to a session. This is being done in the "review" controller. The ReviewHolder class is included in the classes/controller/review.php file. I am using Kohana version 3.2.

The code

$session = Session::instance();
$reviewholder = $session->get('reviewholder');
if($reviewholder == null) {
    $session->set('reviewholder', new ReviewHolder());
}

The problem

The problem is that after I execute the above code, the session gets corrupted and almost the entire website stops functioning correctly. This is the error message on every page:

Session_Exception [ 1 ]: Error reading session data.

The weird part

All pages that are loaded from the "review" controller are still all function correctly! It looks like the above code is messing up every other session in the application...

Debug

I have debugged the session to see if the object is set correctly with the following code:

echo Debug::vars($session->get('reviewholder'));

This shows the object correctly, so it has been set in the session.


Can someone help me out here? Thanks!

Was it helpful?

Solution

It doesn't really matter whats inside the session file.

All i know is that you can only store objects that are serialized in the session.

After serializing you can store it in an session and to make it an object again for later use. You can unserialize the serialized object.

Hope this helps!

OTHER TIPS

I found the following worked for me when storing a custom object in the session:

  1. Make sure your session directory is set correctly in PHP, ie. the directory exists, permissions allow your web server to write to the directory

  2. Force the class for your object to be loaded in bootstrap.php by adding a line such as: require_once( Kohana::find_file('classes','Library/MyClass') );

After those two steps I no longer saw the session corrupt exception

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top