$_SESSION variables and missing Objects

I while ago I wrote a fairly complex script that required user interaction at several points and eventually feeding the results into several csv files. To accomplish this I was having to store objects in session variables and then retrieve them when needed. All was going well, the script was working as far as running but when the csv files were opened they contained nothing but php errors complaining about not being able to access the objects functions. Not good, especially as this particular piece of code was needed the following day. After checking everything, twice, scratching my head a lot and a lengthy google search I eventually stumbled across a somewhat illusive reason for the strange behaviour:

Problem:

Disappearing objects when called from session variables

Error:

PHP errors are generated becuase you're trying to call methods that don't exist because the objects are no longer there.

Cause:

This problem occurs when class definitions are declared after your session has been started.

Solution:

Make sure classes are included or required before you start any sessions used on the site.

require_once "contactsClass.php";
require_once "menuClass.php";
require_once "pageClass.php";

if(!session_id())
{
	session_start();
	header("cache-control: private");
}

Add new comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.