Page 1 of 1

[SOLVED] session handling misunderstanding ?

Posted: Thu Sep 09, 2004 9:15 am
by jakobdoppler
Hi

I have problems with correct implementation of user defined session handling. For my indvidual session handler I use a standard class [1] which stores session data in the database.

In a first page everything works as expected. I start a new instance of the class (which define the and initalize the session_set_save_handler functions), and use $_SESSION[] to write some content to the db

Code: Select all

<?php
require_once 'DB.php';	
require("sessionclass.php");       
//... start dB connection 
	
$session = new session($nikaDb);
session_start();
	
$_SESSION['hello']="dsadsa";
$_SESSION['hello2']=432432143;
echo session_id()."<pre>".print_r($_SESSION,true)."</pre>";
?>
Additionally I use a second page, that is identical to the first one, despite the fact that i don't use the 2 lines that define 'hello' and 'hello2' in the session.
So when I switch from the first page to the second, the session_id is both the same, and the dB tabel is still filled with the 'hello' and 'hello2' vars, but when I try to print out the session (e.g $_SESSION['hello'] or print_r($_SESSION)), it is empty.

Have I misunderstood the session concept ? Can't I simply recall a value with $_SESSION[''] like i do it with the default session handler ? How can I recall the values ? Thx _yak

[1] http://www.nika.at/test/session_handler/session.html

Posted: Thu Sep 09, 2004 12:39 pm
by feyd
the session read function may be accidently erasing the data.

Posted: Thu Sep 09, 2004 5:38 pm
by Roja
Since you didnt provide page1 and 2, but provided a description, I'm going to take you literally:
that is identical to the first one, despite the fact that i don't use the 2 lines that define 'hello' and 'hello2' in the session.
In that case, the problem would be line 6 and 7. Those are re-starting the session, even if one exists, on the second page.

simply comment out line 6, and it should work fine.

Also, why does line six use $session, while everything else uses the correct $_SESSION ?

Confusing naming to be sure..

Posted: Fri Sep 10, 2004 3:48 am
by jakobdoppler
Ok guys, thanks, I really hate being stupid.
Solution to this problem:
Since the Pear:DB class is used in this databased session handler, one have to set a certain FetchMode with

Code: Select all

<?php
$nikaDb->setFetchMode(DB_FETCHMODE_ASSOC); 
?>
That ensures that the fetched rows are saved in an associative array

Code: Select all

<?php
$row['ses_id']
$row['ses_time']
$row['ses_value']
?>
in comparison to an indexed array. The session class code makes use of associative arrays.

Code: Select all

<?php
$row[0]
$row[1]
$row[2]
?>
So the final code would be

Code: Select all

<?php
	require_once('***conf***');
	require_once 'DB.php';	// PEAR:: DB classes.
	require("sessionclass.php"); 
	
	$nikaDb = DB::connect(***conf***, TRUE);
	if(DB::isError($nikaDb)) {
			die($nikaDb->getMessage());	
		}
	$nikaDb->setFetchMode(DB_FETCHMODE_ASSOC);
	
	$session = new session($nikaDb);
	session_start();
	
	$_SESSION['gunter']="dsadsadsa";
	$_SESSION['helmut']=432432143;
?>
If you jump to a file with exactly (sic!) the same content, but without line 15,16 the $_SESSION[] still contains the variables. Thats the way to go :-D

_yak