PHP数据库保存session


lifeTime = get_cfg_var(“session.gc_maxlifetime”);
// open database-connection
$dbHandle = @mysql_connect(“localhost”,”root”,”root”);
$dbSel = @mysql_select_db(“shao”,$dbHandle);
// return success
if(!$dbHandle || !$dbSel)
return false;
$this->dbHandle = $dbHandle;
return true;
}
function close() {
$this->gc(ini_get(‘session.gc_maxlifetime’));
// close database-connection
return @mysql_close($this->dbHandle);
}
function read($sessID) {
// fetch session-data
$res = mysql_query(“SELECT session_data AS d FROM ws_sessions
WHERE session_id = ‘$sessID’
AND session_expires > “.time(),$this->dbHandle);
// return data or an empty string at failure
if($row = mysql_fetch_assoc($res))
return $row[‘d’];
return “”;
}
function write($sessID,$sessData) {
// new session-expire-time
$newExp = time() + $this->lifeTime;
// is a session with this id in the database?
$res = mysql_query(“SELECT * FROM ws_sessions
WHERE session_id = ‘$sessID'”,$this->dbHandle);
// if yes,
if(mysql_num_rows($res)) {
// …update session-data
mysql_query(“UPDATE ws_sessions
SET session_expires = ‘$newExp’,
session_data = ‘$sessData’
WHERE session_id = ‘$sessID'”,$this->dbHandle);
// if something happened, return true
if(mysql_affected_rows($this->dbHandle))
return true;
}
// if no session-data was found,
else {
// create a new row
mysql_query(“INSERT INTO ws_sessions (
session_id,
session_expires,
session_data)
VALUES(
‘$sessID’,
‘$newExp’,
‘$sessData’)”,$this->dbHandle);
// if row was created, return true
if(mysql_affected_rows($this->dbHandle))
return true;
}
// an unknown error occured
return false;
}
function destroy($sessID) {
// delete session-data
mysql_query(“DELETE FROM ws_sessions WHERE session_id = ‘$sessID'”,$this->dbHandle);
// if session was deleted, return true,
if(mysql_affected_rows($this->dbHandle))
return true;
// …else return false
return false;
}
function gc($sessMaxLifeTime) {
// delete old sessions
mysql_query(“DELETE FROM ws_sessions WHERE session_expires < ".time(),$this->dbHandle);
// return affected rows
return mysql_affected_rows($this->dbHandle);
}
}
$session = new session();
session_set_save_handler(array(&$session,”open”),
array(&$session,”close”),
array(&$session,”read”),
array(&$session,”write”),
array(&$session,”destroy”),
array(&$session,”gc”));
session_start();
// etc…
?>
//===============================下面这个是函数式的
unix_timestamp(date_add(now(),interval 1 hour))”;
$sth = mysql_query($stmt);
if($sth)
{
$row = mysql_fetch_array($sth);
return($row[‘session_data’]);
}
else
{
return $sth;
}
}
function on_session_write($key, $val) {
error_log(“$key = $value”);
$val = addslashes($val);
$insert_stmt = “insert into sessions values(‘$key’, “;
$insert_stmt .= “‘$val’,unix_timestamp(date_add(now(), interval 1 hour)))”;
$update_stmt = “update sessions set session_data =’$val’, “;
$update_stmt .= “session_expiration = unix_timestamp(date_add(now(), interval 1 hour))”;
$update_stmt .= “where session_id =’$key ‘”;
// First we try to insert, if that doesn’t succeed, it means
// session is already in the table and we try to update

mysql_query($insert_stmt);
$err = mysql_error();
if ($err != 0)
{
error_log( mysql_error());
mysql_query($update_stmt);
}
}
function on_session_destroy($key) {
mysql_query(“delete from sessions where session_id = ‘$key'”);
}
function on_session_gc($max_lifetime)
{
mysql_query(“delete from sessions where unix_timestamp(session_expiration) < unix_timestamp(now())"); } // Set the save handlers session_set_save_handler("on_session_start", "on_session_end", "on_session_read", "on_session_write", "on_session_destroy", "on_session_gc"); session_start(); ?>



发表评论

电子邮件地址不会被公开。 必填项已用*标注

+ 68 = 70