将session保存到数据库

发布时间:2013-12-10 18:40:25 阅读:1042次
 转
<?php 
/**
http://zhidao.baidu.com/question/93806804.html
bool session_set_save_handler ( string open, string close, string read, string write, string destroy, string gc ) 
 
自定义Session存储方式 
函数中一个参数对应一个函数,函数的名称自定但参数为固定格式,没有用到也要。 
 
一、open(string save_path, string session_name)为session_start 参数string save_path session存取路径 string session_name传递session id的cookie名字。 
function _session_open(string save_path, string name) 
$db=mysql_connect("localhost","root","123456","tendao"); 
return TRUE; 
 
二、close为session_close 无参数 
在此对应关闭数据库,但一般网站中在此一般不要关闭。 
 
三、read(key)为读取session键值。key对应session id。 
 
四、write(key,date)其中的data对应设置的session变量,格式如下: 
email_name|s:13:"qqtxt@163.com";member_id|s:1:"1"; 
 
五、destroy(key)注销session 
在此程序段中对应删除对应记录项。 
 
六、gc(expiry_time)清除过期session记录。 
 
 
*/
class Session{ 
    var $expiry=60; 
    var $db; 
 
    function __construct(){ 
        $this->Session(); 
    } 
 
    function Session(){ 
        session_set_save_handler( array (& $this, "_session_open"), 
        array (& $this, "_session_close"), 
        array (& $this, "_session_read"), 
        array (& $this, "_session_write"), 
        array (& $this, "_session_destroy"), 
        array (& $this, "_session_gc") 
        ); 
    } 
 
    /** 
    * open session handler 
    * 
    * @param string $save_path 
    * @param string $session_name 
    * @return boolen 
    */ 
    function _session_open($save_path, $session_name) 
    { 
        echo "start";
        echo "<br>";
        $this->db=mysql_connect("localhost","root","123456") or die("数据库连接失败!"); 
        mysql_select_db("php_session",$this->db) or die("can't collect db"); 
        return TRUE; 
    } 
 
    function _session_close(){ 
        echo "end";
        echo "<br>";
        return true; 
    } 
 
    function _session_read($key){ 
        echo "read";
        echo "<br>";
        $expiry=time(); 
        $s_query=sprintf("select session_data from session where session_key= '%s' and session_expiry > %d " , $key, $expiry ); 
        $result=mysql_query($s_query,$this->db); 
        $row=mysql_fetch_assoc($result); 
        if($row){ 
            echo "yes";
            echo "<br>";
            return $row['session_data']; 
        }else{ 
            echo "no";
            echo "<br>";
            return FALSE; 
        } 
    }
 
    function _session_write($key,$data){ 
        echo "<br>";
        echo "write";
        echo "<br>";
        $expiry_time=time()+$this->expiry; 
        $s_query=sprintf("select session_data from session where session_key= '%s'", $key); 
        $result=mysql_query($s_query,$this->db); 
        if(mysql_num_rows($result)==0){ 
echo "add";
echo "<br>";
            $s_query=sprintf("insert into session values('%s','%s', %d)",$key,$data,$expiry_time); 
            $result=mysql_query($s_query,$this->db); 
        }else{ 
            $s_query=sprintf("update session set session_key='%s', session_data='%s',session_expiry=%d where session_key='%s'",$key,$data,$expiry_time,$key); 
            $result=mysql_query($s_query,$this->db); 
    } 
        return $result; 
    } 
 
    function _session_destroy($key){ 
        echo "destroy";
        echo "<br>";
        $s_query=sprintf("delete from session where session_key= '%s'", $key); 
        $result=mysql_query($s_query,$this->db); 
        return $result; 
    } 
 
    function _session_gc($expiry_time){ 
        echo "gc";
        echo "<br>";
        $expiry_time=time(); 
        $s_query=sprintf("delete from session where session_expiry < %d", $expiry_time); 
        $result=mysql_query($s_query,$this->db); 
        return $result; 
    } 
 
$_ses_=new Session(); 
session_start(); 
echo session_name."=>".session_id();
echo "<br>";
$_SESSION['time']=time()+1200; 
echo strval(time()).'<br/>'.strval($_SESSION['time']); 
?>

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:将session保存到数据库 出自老鄢博客 | 欢迎分享