Everything serializes, right? Not all the time, more information soon.
Basically, what I was trying to do was to serialize an object into SESSION and pull it later on. Here’s what I did for a test:
-
class test
-
{
-
//some generic code here
-
}
-
-
if (isset($_GET['id'])) {
-
var_dump($_SESSION);
-
var_dump(unserialize($_SESSION['test']));
-
} else {
-
unset($_SESSION['test']);
-
$test = new test();
-
$_SESSION['test'] = $test;
-
header('Location: test.php?id=1');
-
exit();
-
}
All this is is a quick hack that lets me both instantiate the class, set it to session, and look at another page to see if it is still set. When I run this code, the class was being output just fine, meaning that it serialized just fine. However, when I tried to serialize one of my classes (MyClass) in the same code, what I got after serializing was:
-
N;
This is less than useful.
I found on the php site, after quite a bit of frustrated searching, this post on the PHP.net site, that “N;” means basically “can’t serialize that”.
It turns out that my class had a call to __sleep() in it that was stubbed out and never actually implemented. I removed this stubbed out method, which when I serialized my class gave me exactly what I was looking for. I was able to get to any next script and access my object appropriately.
Tags: class test, generic code, hack, header location, new test, sleep, test header, test session, test test, wtf
Sphere: Related Content