View Full Version : losing session object when using header("Location:

01-17-2004, 01:53 PM

I've added the header("Location: http://wheretoforward"); to my security class whereas before I was using javascript to forward, and now my serialized session object is null, anyone know why? or will I be forced to use javascript instead?

Saving the object

# save this object on the session
$_SESSION["securityObj"] = serialize($this);

header("Location: http://".$_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) ."/cm/");

Restoring the object on another page

$sObj = unserialize($_SESSION["securityObj"]);

print "<br>type of securityObj: " . gettype($_SESSION["securityObj"]);
print "<br>Test variable, getUserLoginName(): " . $sObj->getUserLoginName();

If I don't use the header("location: etc...") and use javascript window.location.href = "some place"; then the object is restored without problems and I can access all its members and properties, yet using the header method to forward users results in the object being Null. :confused:

Any ideas welcome

01-17-2004, 04:18 PM
Changing pages within a site won't have an effect on the session since it is a browser session for that site. Have you verified that the value is set before the header is given? And is the URL you are redirecting to still within the site, ie not a another different domain or a subdomain? Also you might want to add this to the top of the pages


This will show any errors that may be happening and are not being displayed.

01-17-2004, 05:56 PM
Yes, the variable is defo set, if I remove the header line, the forwarding is then controlled by javascript and that works ok

All on the same site etc... perhaps... hmmm, the javascript forwards with window.location.href= 'cm/ '; and the header builds the entire string http://localhost/testarea/cm/, that loses the session, I've just added the SID to end of the path using header, and it works ok.