1. 云栖社区>
  2. PHP教程>
  3. 正文

php session session_set_save_handler 机制

作者:用户 来源:互联网 时间:2017-12-01 19:21:11

phpsessionsethandlersave机制

php session session_set_save_handler 机制 - 摘要: 本文讲的是php session session_set_save_handler 机制, php自身带功能,将 session 的数据从之前的files 改变到其他任何地方。比如mysql redis等等,方便你 分布式 的集群应用。 看一下

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;

如果你是一名技术人员可加我QQ 2651-0442-02,如果你是java技术人还可以加入QQ群 1784-9136-0

首发地址:月小升博客 –无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索php , session , set , handler , save 机制 ,以便于您获取更多的相关知识。