Page 1 of 1

$_SESSION not storing values?

Posted: Thu Apr 17, 2008 7:26 am
by Kadanis
Hi

Basically I'm trying to store some vars in the session, now I can write to the session on the www domain, but this is on a subdomain (not sure if that makes a difference).

code is like this, although i've re-written only the pertinent bits.

index.php

Code: Select all

 
<?php
session_start();
 
require_once('lib/Class.php');
 
$test = new Class();
 
header('location: test.php');
die();
?>
 
Class.php

Code: Select all

 
<?php
class Class {

public function __construct(){
    //do some stuff that returns an array and a string
    $_SESSION['info'] = serialize($array);
    $_SESSION['lan'] = $string;
}
 
?>
 
test.php

Code: Select all

 
<?php
 
$info = unserialize($_SESSION['info']);
$lan = $_SESSION['lan'];
 
?>
 
The error returned is

Notice: Undefined index: info in /home/vhosts/dev/test.php on line 4
Notice: Undefined index: lan in /home/vhosts/dev/test.php on line 5

I've done a var_dump of the $_SESSION on each page. On index.php it has all the information it should do. On test.php it's empty. I've checked the session files on the server and although the file is created no information is written to it.

PHP Version is 5.0.4 (upgrading at this time isn't an option).

This is really confusing me. Can anyone shed any light?

Re: $_SESSION not storing values?

Posted: Thu Apr 17, 2008 7:57 am
by John Cartwright

Code: Select all

session_write_close();
header('location: http://sub.domain.tld/test.php');
die();
Typically you should call session_write_close() before a redirect, since the redirect can occur faster than the session handler can write the data. Also, you should always specify the absolute url since not all browsers can handle relative directs.

Re: $_SESSION not storing values?

Posted: Thu Apr 17, 2008 8:03 am
by Kadanis
Thanks for replying, but sadly session_write_close(); didn't work. I'm still getting the errors and not writing to the file.

Re: $_SESSION not storing values?

Posted: Thu Apr 17, 2008 8:29 am
by Kadanis
Just thought I'd add a little more information. I created a test file with the following code in it

Code: Select all

 
session_start();
$_SESSION['userinfo'] = serialize(
    array(
        'username'=>'username',
        'password'=>'nothing',
        'ip_log'=>md5($_SERVER['REMOTE_ADDR']),
        'session_id'=>md5(session_id())
    )
);
header('location: lobby.php');
 
 
This file writes the session data and redirects the page. It only seems to fail when I call $_SESSION from inside the Class. Is there any reason why $_SESSION would fail from inside a class but work from php4 style procedural code.