Use Session in WordPress

Extra attention must be paid to the use of session in wordpress especially when objects are stored. if you are not careful, ‘__PHP_Incomplete_Class’ is waiting to happen.

It is very well documented that to use session in wordpress, you will just need to put

if(!isset($_SESSION))
{
  session_start();
}

code somewhere in your wordpress, the session is NOT started and nor used in DEFAULT install of wordpress. while the user’s login is managed purely on cookies.

But I would say there is much more to it when it comes down to the use of session in wordpress.

1. When to run session_start?

If you want to use session, this must be run before any browser output.

2. Can we store objects in session?

Yes you can, and serialization and deserialization is done by php, but here comes the tricky part, the class definition of objects in session must be loaded before session start, otherwise how could php session handle deserialization? if this is the case you will get __PHP_Incomplete_Class error message.

3. How to avoid __PHP_Incomplete_Class?

The trouble is some sessions are started by plugins, there is no guarantee this plugin’s use of session is not breaking the other one, the responsiblity unfortunately falls directly on the owner of the site, and most of time they are not professional developers.

So if you are having __PHP_Incomplete_Class in wordpress, try to disable some of other plugins to see which ones are conflicting…

On the other hand, try not to use plugins that need sessions unless it is absolutely necessary.

Tags:

This entry was posted on Friday, December 10th, 2010 at 12:29 am and is filed under PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

*