PHP code<?php
ini_set('session.save_handler', 'user');
ini_set('session.use_cookies', 0);
session_set_save_handler('open', 'close', 'read', 'write', 'destroy', 'gc');
$link = null;
$expiry = 86400;
function open($save_path, $session_name)
{
//连接数据库
}
function close() {
return true;
}
function read($sessionId) {
global $link;
$sessionId = mysql_escape_string($sessionId);
$time = time();
$sql = "SELECT value FROM session WHERE session_id='{$sessionId}' AND expiry>{$time}";
$result = mysql_query($sql, $link);
if(@$row = mysql_fetch_row($result, $link)) {
return $row[0];
}else {
return '';
}
}
function write($sessionId, $sessionData) {
global $link, $expiry;
$sessionId = mysql_escape_string($sessionId);
$sessionData = mysql_escape_string($sessionData);
$time = time() + $expiry;
$sql = "INSERT INTO session(session_id, value, expiry) VALUES('{$sessionId}', '{$sessionData}', {$time});";
$result = mysql_query($sql, $link);
if($result) {
return true;
}
$sql = "UPDATE session SET value='{$sessionData}', expiry={$time} WHERE session_id='{$sessionId}'";
$result = mysql_query($sql, $link);
if($result && mysql_affected_rows($link)) {
return true;
}else {
return false;
}
}
function destroy($sessionId) {
global $link;
$sessionId = mysql_escape_string($sessionId);
$sql = "DELETE FROM session WHERE session_id='{$sessionId}';";
$result = mysql_query($sql, $link);
if($result) {
return true;
}else {
return false;
}
}
function gc($maxLifeTime) {
return true;
}