Rumah >pembangunan bahagian belakang >tutorial php > session_set_save_handler()中write使用的有关问题
session_set_save_handler()中write使用的问题
我把SESSION写入数据库,sessionID用URL传。
a.php:
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> <?php require('session.php'); session_start(); $_SESSION['test'] = 1; echo '<a href="b.php?' . session_name() . '=' . session_id() . '">查看';
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> <?php require('session.php'); session_start();
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> <?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; }