Cannot get code to work for session management on a database. Does anyone
have tips. It appears that the data goes into the table, but I can't get it
out. session_start just initializes the data in the value column.:
------------------------
SessionsTable definition
-----------------------
CREATE TABLE `SessionsTable` (
`SID` varchar(32) NOT NULL default '',
`expiration` int(11) NOT NULL default '0',
`value` text NOT NULL,
PRIMARY KEY (`SID`)
) TYPE=MyISAM
------------------------
mysql_sessions.inc:
------------------------
<?php
// Session Table
$sess_table = "SessionsTable";
// Retrieve the session maximum lifetime (found in php.ini)
$lifetime = get_cfg_var("session.gc_maxlifetime");
//=============
// function: mysql_session_open()
// purpose: Opens a persistent server connection and selects the
// database.
//=============
function mysql_session_open($session_path, $session_name) {
mysql_pconnect("localhost", "root", "")
or die("Can't connect to MySQL server! ");
mysql_select_db("globalDB")
or die("Can't select MySQL sessions database");
} // end mysql_session_open()
//=============
// function: mysql_session_close()
// purpose: Doesn't actually do anything since the server connection is
// persistent. Keep in mind that although this function
// doesn't do anything in my particular implementation, I
// still must define it.
//=============
function mysql_session_close() {
return 1;
} // end mysql_session_close()
//=============
// function: mysql_session_select()
// purpose: Reads the session data from the database
//=============
function mysql_session_select($SID) {
GLOBAL $sess_db;
GLOBAL $sess_table;
$query = "SELECT value FROM $sess_table
WHERE SID = '$SID' AND
expiration > ". time();
$result = mysql_query($query);
} // end mysql_session_select()
//=============
// function: mysql_session_write()
// purpose: This function writes the session data to the database. If that
SID
// already exists, then the existing data will be updated.
//=============
function mysql_session_write($SID,$value) {
GLOBAL $sess_db;
GLOBAL $sess_table;
GLOBAL $lifetime;
$expiration = time() + $lifetime;
$query = "INSERT INTO $sess_table
VALUES('$SID', '$expiration', '$value')";
$result = mysql_query($query);
if (! $result) :
$query = "UPDATE $sess_table SET
expiration = '$expiration',
value = '$value' WHERE
SID = '$SID' AND expiration >". time();
$result = mysql_query($query);
endif;
} // end mysql_session_write()
//=============
// function: mysql_session_destroy()
// purpose: deletes all session information having input SID (only one row)
//=============
function mysql_session_destroy($sessionID) {
GLOBAL $sess_table;
$query = "DELETE FROM $sess_table
WHERE SID = '$sessionID'";
$result = mysql_query($query);
} // end mysql_session_destroy()
//=============
// function: mysql_session_garbage_collect()
// purpose: deletes all sessions that have expired.
//=============
function mysql_session_garbage_collect($lifetime) {
GLOBAL $sess_table;
$query = "DELETE FROM $sess_table
WHERE sess_expiration < ".time() - $lifetime;
$result = mysql_query($query);
return mysql_affected_rows($result);
} // end mysql_session_garbage_collect()
?>
--------------------------------
End of mysql_sessions.inc
--------------------------------
-------------------------------
1-1.php - The first page
-------------------------------
<?
INCLUDE("mysql_sessions.inc");
session_set_save_handler("mysql_session_open", "mysql_session_close",
"mysql_session_select", "mysql_session_write",
"mysql_session_destroy",
"mysql_session_garbage_collect");
// create a new session
session_start();
// register a session-variable
session_register("bgcolor");
// Assign a value to the session-variable
$_SESSION['bgcolor'] = "#8080ff";
?>
<html>
<head>
<title>Session Example #1</title>
</head>
<body bgcolor="<?=$_SESSION['bgcolor']?>" text="#000000" link="#000000"
vlink="#000000" alink="#000000">
Welcome to a session-enabled page! The background color on the next page
will be set to a stylish blue.<p>
<a href = "1-2.php">Go to another session-enabled page</a>.
</body>
</html>
-----------------------
End of 1-1.php
-----------------------
-----------------------
1-2.php - To confirm that the session worked
-----------------------
<?
INCLUDE("mysql_sessions.inc");
session_set_save_handler("mysql_session_open", "mysql_session_close",
"mysql_session_select", "mysql_session_write",
"mysql_session_destroy",
"mysql_session_garbage_collect");
// Resume session created in Listing 1-2
session_start();
?>
<html>
<head>
<title>Session Example #1</title>
</head>
<body bgcolor="<?=$_SESSION['bgcolor']?>" text="#000000" link="#808040"
vlink="#606060" alink="#808000">
<?
// Display the value of the $bgcolor variable.
printf ("The persistent background color is: %s <br>\n",
$_SESSION['bgcolor']);
?>
</body>
</html>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php