CREATE TABLE `sessions` (
`session_key` char(32) NOT NULL,
`session_expire` int(11) unsigned NOT NULL,
`session_value` text NOT NULL,
PRIMARY KEY (`session_key`)
)
Step2 : Create A connection file
configure.php
$HOST = "localhost";
$DBNAME="sessions";
$USER = "nanhe";
$PASS = "chapra";
$HANDLER = "";
$LIFETIME = get_cfg_var("session.gc_maxlifetime");
?>
Step3 : Create handler
handler.php
include("configure.php");
function sessionOpen($save_path, $session_name){
global $HOST, $DBNAME, $USER, $PASS, $HANDLER;
if (!$HANDLER = mysql_pconnect($HOST, $USER, $PASS)) {
echo("
echo("
die;
}
if (! mysql_select_db($DBNAME, $HANDLER)) {
echo("
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(mysql_error());
if (mysql_numrows($session_session_value) == 1) {
return mysql_result($session_session_value, 0);
} else {
return false;
}
}
function sessionWrite($session_key, $val)
{
global $session;
echo "The value=".$val;
$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(mysql_error());
} else {
$return = mysql_query("UPDATE sessions
SET session_value = '$val',
session_expire = UNIX_TIMESTAMP(NOW())
WHERE session_key = '$session_key'")
or die(mysql_error());
if (mysql_affected_rows() < 0) {
echo("We were unable to update session
session_value for session $session_key");
}
}
return $return;
}
function sessionDestroy($session_key)
{
global $session;
$session_key = addslashes($session_key);
$return = mysql_query("DELETE FROM sessions
WHERE session_key = '$session_key'")
or die(mysql_error());
return $return;
}
function sessionGc($maxlifetime)
{
global $session;
$expirationTime = time() - $maxlifetime;
$return = mysql_query("DELETE FROM sessions WHERE session_expire <
$expirationTime") or die("sessionGc Function".mysql_error());
return $return;
}
session_set_save_handler("sessionOpen", "sessionClose", "sessionRead", "sessionWrite", "sessionDestroy", "sessionGc");
?>
Step4 : Test your program
No comments:
Post a Comment