Jump to content

Modify Sessions Help


Wstar

Recommended Posts

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

[code]$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"));
?>
[/code]


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

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

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

so it now looks like this:

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

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:

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

Why do I get this error?
[quote]
Fatal error: session_start(): Failed to initialize storage module: user (path: /tmp) in /var/www/html/includes/navigation.php on line 13[/quote]

Thanks for any help anyone can give me. 

|Wstar|
Link to comment
Share on other sites

[quote author=Wstar link=topic=104666.msg417603#msg417603 date=1155843741]
One last question.  When I write the following code:

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

Why do I get this error?
[quote]
Fatal error: session_start(): Failed to initialize storage module: user (path: /tmp) in /var/www/html/includes/navigation.php on line 13[/quote]

|Wstar|
[/quote]

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.