database sessions: data field not being set
09-13-2008, 05:36 PM
|
database sessions: data field not being set
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
I've been trying to use database sessions in a project I'm working on but so far its not working out. The session is created just fine, but it refuses to store anything. When I look at the database entry the session_data field is always empty.
Here is the code I'm using to manage my sessions:
PHP Code:
<?php
class SessionManager {
var $life_time;
var $conn;
function __construct()
{
// Read the maxlifetime setting from PHP
$this->life_time = get_cfg_var("session.gc_maxlifetime");
// Register this object as the session handler
session_set_save_handler(
array( &$this, "open" ),
array( &$this, "close" ),
array( &$this, "read" ),
array( &$this, "write"),
array( &$this, "destroy"),
array( &$this, "gc" ));
$this->conn = mysql_connect('host', 'user', 'pass');
mysql_select_db('table', $this->conn);
}
function open( $save_path, $session_name )
{
global $sess_save_path;
$sess_save_path = $save_path;
// Don't need to do anything. Just return TRUE.
return true;
}
function close()
{
return true;
}
function read( $id )
{
// Set empty result
$data = '';
// Fetch session data from the selected database
$time = time();
$newid = @mysql_real_escape_string($id, $this->conn);
$sql = "SELECT `session_data` FROM `sessions` WHERE
`session_id` = '$newid' AND `expires` > $time";
$result = @mysql_query($sql, $this->conn);
$count = @mysql_num_rows($result);
if($count > 0)
{
$row = @mysql_fetch_assoc($result, MYSQL_ASSOC);
$data = $row['session_data'];
}
return $data;
}
function write( $id, $data )
{
// Build query
$time = time() + $this->life_time;
$newid = mysql_real_escape_string($id, $this->conn);
$newdata = mysql_real_escape_string($data, $this->conn);
$sql = "REPLACE INTO `sessions`
(`session_id`,`session_data`,`expires`) VALUES('$newid',
'$newdata', $time)";
if(!$rs = @mysql_query($sql, $this->conn))
return false;
return true;
}
function destroy( $id ) {
// Build query
$newid = mysql_real_escape_string($id, $this->conn);
$sql = "DELETE FROM `sessions` WHERE `session_id` =
'$newid'";
@mysql_query($sql, $this->conn);
return true;
}
function gc() {
// Garbage Collection
// Build DELETE query. Delete all records who have passed the expiration time
$sql = 'DELETE FROM `sessions` WHERE `expires` <
UNIX_TIMESTAMP();';
@mysql_query($sql, $this->conn);
// Always return TRUE
return true;
}
}
?>
and here is the code I'm using to test it:
PHP Code:
require_once('include/session.php');
$sess = new SessionManager();
session_start();
$_SESSION['username'] = $username;
header('Location: redirect.php?loc=home');
PHP Code:
require_once('include/session.php');
$sess = new SessionManager();
session_start();
echo $_SESSION['username'];
In this case there is no output and when I look in the database this is what I see:
Code:
session_id | session_data | expires
27d13b0d405f9c9547fe5090baa34d6f 1221342837
If anyone has some insight as to what my problem is I'd appreciate it.
Last edited by NullPointer; 09-13-2008 at 05:42 PM..
|
|
|
|
09-13-2008, 06:17 PM
|
Re: database sessions: data field not being set
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
I personally used this, with a mysql db some years ago:
It may not be the cleanest code, nor totally fool proofed, but it worked ok.
I didn't see anything wrong in your code, but I'm a bit sleepy, so it may have passed through.
In a prepended file, to declare the:
PHP Code:
session_set_save_handler('_sessOpen', '_sessClose', '_sessRead', '_sessWrite', '_sessDestroy', '_sessClean'); session_start();
mysqlSession.php:
PHP Code:
<?php
/********************************************* ** $RCSfile: auth.php,v $ ** $Revision: 157 $ ** $Date: 2006-10-11 19:27:36Z $ ** $Author: tmo $ ** Client : fcs ** Creation date: 10 août 06 14:35:07 ** Usage : ** Short description : *********************************************/
/* * MYSQL session save handler * Store the sessions in an SQL database. *
CREATE TABLE sessions ( id varchar(32) NOT NULL, access int(10) unsigned, data text, PRIMARY KEY (id) ); */ $GLOBALS['debug']=FALSE; function _sessOpen() { if($GLOBALS['debug']===TRUE){ echo"<pre>".print_r(func_get_args(),1)."</pre>"; _sessReport('open'); } $db=sessDbConnect(); if(!$GLOBALS['_sess_db']){ echo mysql_error(); die('_open'); } else{ return true; } return FALSE; }
function sessDbConnect(){ $db=mysql_connect(DB_HOST, DB_USER, DB_PWD); $tbl=mysql_select_db(DB_NAME, $db); $GLOBALS['_sess_db']=&$db; return $db; }
function _sessClose(){ if($GLOBALS['debug']===TRUE){ echo"<pre>".print_r(func_get_args(),1)."</pre>"; _sessReport('close'); } return mysql_close(sessDbConnect()); }
function _sessRead($id){ if($GLOBALS['debug']===TRUE){ echo"<pre>".print_r(func_get_args(),1)."</pre>"; _sessReport('read'); } $id = mysql_real_escape_string($id); $sql = "SELECT data FROM sessions WHERE id = '$id'"; if ($result = mysql_query($sql, sessDbConnect())){ if (mysql_num_rows($result)){ $record = mysql_fetch_assoc($result); return $record['data']; } } else{ echo mysql_error(); die('_READ'); } return ''; }
function _sessWrite($id, $data){ if($GLOBALS['debug']===TRUE){ echo"<pre>".print_r(func_get_args(),1)."</pre>"; _sessReport('write'); } $access = time(); $id = mysql_real_escape_string($id); $access = mysql_real_escape_string($access); $data = mysql_real_escape_string($data); $sql = "REPLACE INTO sessions VALUES ('$id', '$access', '$data')"; $ret=mysql_query($sql, sessDbConnect()); if(!$ret){ echo mysql_error(); die('_WRITE'); } return $ret; }
function _sessDestroy($id){ if($GLOBALS['debug']===TRUE){ echo"<pre>".print_r(func_get_args(),1)."</pre>"; _sessReport('destroy'); } $id = mysql_real_escape_string($id); $sql = "DELETE FROM sessions WHERE id = '$id'"; $ret=mysql_query($sql, sessDbConnect()); if(!$ret){ echo mysql_error(); die('_DESTROY'); } return $ret; }
function _sessClean($max){ if($GLOBALS['debug']===TRUE){ echo"<pre>".print_r(func_get_args(),1)."</pre>"; _sessReport('clean'); } $old = time() - $max; $old = mysql_real_escape_string($old); $sql = "DELETE FROM sessions WHERE access < '$old'"; $ret=mysql_query($sql, sessDbConnect()); if(!$ret){ echo mysql_error(); die('_CLEAN'); } return $ret; }
function _sessReport($str){ //echo("\n<br/>".$str); }
__________________
Only a biker knows why a dog sticks his head out the window.
Last edited by tripy; 09-13-2008 at 06:25 PM..
|
|
|
|
09-13-2008, 08:57 PM
|
Re: database sessions: data field not being set
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
I tried your code in place of mine and I have the same problem.
|
|
|
|
|
« Reply to database sessions: data field not being set
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|