Jump to content


Photo

Modify Sessions Help


  • Please log in to reply
2 replies to this topic

#1 Wstar

Wstar
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 17 August 2006 - 07:42 PM

I'm having a problem writing to a session.  Here is my customer session handler:

$SESS_DBHOST = "";			/* database server hostname */
$SESS_DBNAME = "";			/* database name */
$SESS_DBUSER = "";		/* database user */
$SESS_DBPASS = "";		/* database password */

$SESS_DBH = "";
$SESS_LIFE = get_cfg_var("session.gc_maxlifetime");

function sess_open($save_path, $session_name) {
	global $SESS_DBHOST, $SESS_DBNAME, $SESS_DBUSER, $SESS_DBPASS, $SESS_DBH;

	if (! $SESS_DBH = mysql_pconnect($SESS_DBHOST, $SESS_DBUSER, $SESS_DBPASS)) {
		echo "<li>Can't connect to $SESS_DBHOST as $SESS_DBUSER";
		echo "<li>MySQL Error: ", mysql_error();
		die;
	}

	if (! mysql_select_db($SESS_DBNAME, $SESS_DBH)) {
		echo "<li>Unable to select database $SESS_DBNAME";
		die;
	}

	return true;
}

function sess_close() {
	return true;
}

function sess_read($key) {
	global $SESS_DBH, $SESS_LIFE;

	$qry = "SELECT value FROM sessions WHERE sesskey = '$key' AND expiry > " . time();
	$qid = mysql_query($qry, $SESS_DBH);

	if (list($value) = mysql_fetch_row($qid)) {
		return (string)$value;
	}

	return (string)"";
}

function sess_write($key, $val) {
	global $SESS_DBH, $SESS_LIFE;

	$expiry = time() + $SESS_LIFE;
	$value = addslashes($val);

	$qry = "INSERT INTO sessions VALUES ('$key', $expiry, '$value')";
	$qid = mysql_query($qry, $SESS_DBH);

	if (! $qid) {
		$qry = "UPDATE sessions SET expiry = $expiry, value = '$value' WHERE sesskey = '$key' AND expiry > " . time();
		$qid = mysql_query($qry, $SESS_DBH);
	}

	return $qid;
}

function sess_destroy($key) {
	global $SESS_DBH;

	$qry = "DELETE FROM sessions WHERE sesskey = '$key'";
	$qid = mysql_query($qry, $SESS_DBH);

	return $qid;
}

function sess_gc($maxlifetime) {
	global $SESS_DBH;

	$qry = "DELETE FROM sessions WHERE expiry < " . time();
	$qid = mysql_query($qry, $SESS_DBH);

	return mysql_affected_rows($SESS_DBH);
}

session_set_save_handler(
	"sess_open",
	"sess_close",
	"sess_read",
	"sess_write",
	"sess_destroy",
	"sess_gc");
session_start();
sess_gc(get_cfg_var("session.gc_maxlifetime"));
?>


At the begining of my site I have the following.
include("includes/session_mysql.php");
require('includes/layout_top.php');
$_SESSION['user']="nli";  //  nli = Not Logged In

When I check my session user in my database it shows nli for the user.  When i add the line:

$_SESSION['user'] = "Bob";

so it now looks like this:

include("includes/session_mysql.php");
require('includes/layout_top.php');
$_SESSION['user']="nli";  //  nli = Not Logged In
$_SESSION['user'] = "Bob";

When i check the session now when i reload the page.  It still shows nli for the user in the mysql database.  Why does the session not re-write Bob over nli?

I've tried to do the following with no luck:
  a) Delete the session before I tried to rename the user
  b) unset() the session before I tried to rename the user
  c) session_unset() before i tried to rename the user

Why will this not work?  I'm just not getting this. 

One last question.  When I write the following code:

$_SESSION['user']="nli";  //  nli = Not Logged In
session_destroy();
$_SESSION['user'] = "Bob";

Why do I get this error?

Fatal error: session_start(): Failed to initialize storage module: user (path: /tmp) in /var/www/html/includes/navigation.php on line 13


Thanks for any help anyone can give me. 

|Wstar|

#2 dgerler

dgerler
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 19 August 2006 - 10:01 AM

One last question.  When I write the following code:

$_SESSION['user']="nli";  //  nli = Not Logged In
session_destroy();
$_SESSION['user'] = "Bob";

Why do I get this error?

Fatal error: session_start(): Failed to initialize storage module: user (path: /tmp) in /var/www/html/includes/navigation.php on line 13


|Wstar|


I don't know about the first portion of your post, but in this last part.. don't you mean to use:

sess_destroy(); instead of  session_destroy();

I mean you are using a custom session handler, right?

Dave


#3 Wstar

Wstar
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 22 August 2006 - 03:00 PM

Yeah, i'm using a custom handler.  Is there a nice custom handler out there i can get?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users