$_SESSION not storing values?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

$_SESSION not storing values?

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: $_SESSION not storing values?

Post 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.
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Re: $_SESSION not storing values?

Post 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.
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Re: $_SESSION not storing values?

Post 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.
Post Reply