php自身带功能,将 session 的数据从之前的files 改变到其他任何地方。比如mysql redis等等,方便你 分布式 的集群应用。 看一下原理 <?php /** *Session open (called by session_start( )) *Session close (called at page end) *Session read (called after session_start( ) ) *Session write (called when session data is to be written) *Session destroy (called by session_destroy( ) ) *Session garbage collect (called randomly) */ function sess_open($sess_path, $sess_name) { print "Session opened.\n"; print "Sess_path: $sess_path\n"; print "Sess_name: $sess_name\n\n"; return true; } function sess_close( ) { print "Session closed.\n"; return true; } function sess_read($sess_id) { print "Session read.\n"; print "Sess_ID: $sess_id\n"; return ''; } function sess_write($sess_id, $data) { print "Session value written.\n"; print "Sess_ID: $sess_id\n"; print "Data: $data\n\n"; return true; } function sess_destroy($sess_id) { print "Session destroy called.\n"; return true; } function sess_gc($sess_maxlifetime) { print "Session garbage collection called.\n"; print "Sess_maxlifetime: $sess_maxlifetime\n"; return true; } session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc"); session_start( ); $_SESSION['foo'] = "bar"; print "Some text\n"; $_SESSION['baz'] = "wombat"; ?> <?php /* 在使用session前 将之前的session_start() 改成 require_once("session.class.php"); */ $SESS_DBHOST = "localhost"; /* database server hostname */ $SESS_DBNAME = "bigclient"; /* database name */ $SESS_DBUSER = "urldb"; /* database user */ $SESS_DBPASS = "123456"; /* database password */ $SESS_DBH = ""; $SESS_LIFE = get_cfg_var("session.gc_maxlifetime"); function sess_open($save_path, $session_name) { global $SESS_DBHOST, $SESS_DBNAME, $SESS_DBUSER, $SESS_DBPASS, $SESS_DBH; if (! $SESS_DBH = @mysql_pconnect($SESS_DBHOST, $SESS_DBUSER, $SESS_DBPASS)) { echo "<li>Can't connect to $SESS_DBHOST as $SESS_DBUSER"; echo "<li>MySQL Error: " . mysql_error(); die; } if (! mysql_select_db($SESS_DBNAME, $SESS_DBH)) { echo "<li>Unable to select database $SESS_DBNAME"; die; } return true; } function sess_close() { return true; } function sess_read($key) { global $SESS_DBH, $SESS_LIFE; $qry = "SELECT value FROM session_tbl WHERE sesskey = '$key' AND expiry > " . time(); $qid = mysql_query($qry, $SESS_DBH); if (list($value) = mysql_fetch_row($qid)) { return $value; } return false; } function sess_write($key, $val) { global $SESS_DBH, $SESS_LIFE; $expiry = time() + $SESS_LIFE; //过期时间 $value = addslashes($val); $qry = "INSERT INTO session_tbl VALUES ('$key', $expiry, '$value')"; $qid = mysql_query($qry, $SESS_DBH); if (! $qid) { $qry = "UPDATE session_tbl SET expiry = $expiry, value = '$value' WHERE sesskey = '$key' AND expiry > " . time(); $qid = mysql_query($qry, $SESS_DBH); } return $qid; } function sess_destroy($key) { global $SESS_DBH; $qry = "DELETE FROM session_tbl WHERE sesskey = '$key'"; $qid = mysql_query($qry, $SESS_DBH); return $qid; } function sess_gc($maxlifetime) { global $SESS_DBH; $qry = "DELETE FROM session_tbl WHERE expiry < " . time(); $qid = mysql_query($qry, $SESS_DBH); return mysql_affected_rows($SESS_DBH); } session_set_save_handler( "sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc"); session_start(); ?> 使用方法: 在使用 session 前 将之前的session_start() 改成 require_once("session.class.php"); CREATE TABLE IF NOT EXISTS `session_tbl` ( `sesskey` varchar(255) NOT NULL, `expiry` varchar(255) NOT NULL, `value` varchar(255) NOT NULL, UNIQUE KEY `sesskey_2` (`sesskey`), KEY `sesskey` (`sesskey`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;(责任编辑:好模板) |