Try these snippets of code. It works for me🙂
//handler.php
<?php
$HOST = "localhost";
$DBNAME="sessions";
$USER = "sessionmanager";
$PASS = "sessionmanager";
session_start();
//The name of the MySQL handler/pointer we will be using, and the lifetime of the session as set by php.ini
$HANDLER = "";
$LIFETIME = get_cfg_var("session.gc_maxlifetime");
function sessionOpen($save_path, $session_name)
{
global $HOST, $DBNAME, $USER, $PASS, $HANDLER;
if (!$HANDLER = mysql_pconnect($HOST, $USER, $PASS)) {
echo("<li>Can't connect to $HOST as $USER");
echo("<li>MySQL Error: " . mysql_error());
die;
}
if (! mysql_select_db($DBNAME, $HANDLER)) {
echo("<li>We were unable to select database $DBNAME");
die;
}
return true;
}
function sessionClose()
{
return true;
}
function sessionRead($session_key)
{
global $session;
$session_key = addslashes($session_key);
$session_session_value =
mysql_query("SELECT session_value
FROM sessions WHERE session_key = '$session_key'")
or die(db_error_message());
if (mysql_numrows($session_session_value) == 1) {
return mysql_result($session_session_value, 0);
} else {
return false;
}
}
function sessionWrite($session_key, $val)
{
global $session;
$session_key = addslashes($session_key);
$val = addslashes($val);
$session = mysql_result(mysql_query("SELECT COUNT(*) FROM sessions
WHERE session_key = '$session_key'"), 0);
if ($session == 0) {
$return =
mysql_query("INSERT INTO sessions
(session_key, session_expire, session_value)
VALUES ('$session_key',
UNIX_TIMESTAMP(NOW()), '$val')")
or die(db_error_message());
} else {
$return = mysql_query("UPDATE sessions
SET session_value = '$val',
session_expire = UNIX_TIMESTAMP(NOW())
WHERE session_key = '$session_key'")
or die(db_error_message());
if (mysql_affected_rows() < 0) {
echo("We were unable to update session
session_value for session $session_key");
}
}
return $return;
}
function sessionDestroyer($session_key)
{
global $session;
$session_key = addslashes($session_key);
$return = mysql_query("DELETE FROM sessions
WHERE session_key = '$session_key'")
or die(db_error_message());
return $return;
}
function sessionGc ($maxlifetime)
{
global $session;
$expirationTime = time() - $maxlifetime;
$return = mysql_query("DELETE FROM sessions WHERE session_expire <
$expirationTime") or die(db_error_message());
return $return;
}
session_set_save_handler(
'sessionOpen',
'sessionClose',
'sessionRead',
'sessionWrite',
'sessionDestroyer',
'sessionGc'
);
?>
and try this for session_test.php
//session_test.php
<?
include("handler.php");
session_start();
session_register("count");
$count++;
// Increment our count variable in which we store
// the accesses to the page
if ($action == "destroy") {
session_destroy();
} elseif ($action == "gc") {
// We actually force garbage collection here by
// directly calling our custom handler that does so.
$maxlife = get_cfg_var("session.gc_maxlifetime");
sessionGc($maxlife);
} elseif (!$action) {
echo("No action specified<br>");
} else {
echo("Cannot do $action with the session handlers<br>");
}
?>
<head>
<title>Session Test Functions</title>
</head>
<body>Action: <b><?=$action?></b><br>
Count: <b><?=$count?></b><br><p>
<form action="<?=$PHP_SELF?>" method="POST">
<table border=0>
<tr>
<td>Action:</td>
<td>
<select name="action">
<option value="destroy">Destroy</option>
<option value="gc">Force Garbage Collection</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td><br><input type="submit"></td>
</tr>
</table>
<center>Hit refresh to increment the counter</center>
</form>
</body>
Make sure that your MySQL database that stores your sessions and session information is up and running.
Let me know if you got it.
Cheers,
InduB