PHP __PHP_Incomplete_Class Object with my $_SESSION data

When you’re accessing $_SESSION, you’re not just changing the current script’s copy of the data read from the session, you’re writing SafeString objects back into the active session.

But putting custom objects in the session is dodgy and something I would generally try to avoid. To be able to do it you have to have defined the class in question before calling session_start; if you don’t, PHP’s session handler won’t know how to deserialise the instances of that class, and you’ll end up with the __PHP_Incomplete_Class Object.

So avoid frobbing the session. If you must take this approach, make a copy of the data from $_SESSION into a local $mysession array. However, I have to say I think the whole idea of a SafeString is dangerous and unworkable; I don’t think this approach is ever going to be watertight. Whether a string of raw text is ‘safe’ is nothing to do with where it came from, it is a property of how you encode it for the target context.

If you get another text string from a different source such as the database, or a file, or calculated within the script itself, it needs exactly the same handling as a string that came from the user: it needs to be htmlspecialchars​ed. You’re going to have to write that escape anyway; the safestring gains you nothing. If you need to send the string to a different destination format, you would need a different escape.

You cannot encapsulate all string processing problems into one handy box and never think about them again; that’s just not how strings work.

Leave a Comment