php session session_set_save_handler 机制


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
你将得到的不仅仅是技术的交流,还有职业机会,人生解惑.
首发地址:月小升博客https://java-er.com/blog/php-session-session_set_save_handler/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
此条目发表在 PHP 分类目录,贴了 , , 标签。将固定链接加入收藏夹。
既然来了,就评论一下,不会怀孕的

发表评论

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

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">