首頁 > php教程 > PHP源码 > session保存在mysql

session保存在mysql

PHP中文网
發布: 2016-05-25 17:03:58
原創
1003 人瀏覽過

session保存在mysql 

<?php
require_once("class_mysql.php");

class session
{
    public $db;
    function session(&$db)
    {
        $this -> db = &$db;
        session_module_name(&#39;user&#39;);
        session_set_save_handler(
            array(&$this, &#39;open&#39;),
            array(&$this, &#39;close&#39;),
            array(&$this, &#39;read&#39;),
            array(&$this, &#39;write&#39;),
            array(&$this, &#39;destroy&#39;),
            array(&$this, &#39;gc&#39;)
            );
        session_start();
    } 
    function unserialize($data_value)
    {
        $vars = preg_split(&#39;/([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\|/&#39;, $data_value, -1, 
        PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
            );
        for ($i = 0; isset($vars[$i]); $i++)
        {
            $result[$vars[$i++]] = unserialize($vars[$i]);
        } 
        return $result;
    } 
    function open($path, $name)
    {
        return true;
    } 
    function close()
    {
        return true;
    } 
    function read($session_id)
    {
        $session_id = $this -> db -> escape_string($session_id);
        if ($row = $this -> db -> query("select * from `sessions` where `session_id` = &#39;$session_id&#39; limit 1"))
        {
            return $row[&#39;data_value&#39;];
        } 
        else
        {
            $this -> db -> query("insert into `sessions` set `session_id` = &#39;$session_id&#39;");
            return "";
        } 
    } 
    function write($session_id, $data_value)
    {
        $data = $this -> unserialize($data_value);
        $session_id = $this -> db -> escape_string($session_id);
        $data_value = $this -> db -> escape_string($data_value);
        $sql = "update `sessions` set ";
        if (isset($data[&#39;user_id&#39;]))
        {
            $sql .= "`user_id` = &#39;{$data[&#39;user_id&#39;]}&#39;, ";
        } 
        $sql .= "`data_value` = &#39;$data_value&#39;, "
         . "`last_visit` = null "
         . "where `session_id` = &#39;$session_id&#39;";
        $this -> db -> query($sql);
        return true;
    } 
    function destroy($session_id)
    {
        $session_id = $this -> db -> escape_string($session_id);
        $this -> db -> query("delete from `sessions` where `session_id` = &#39;$session_id&#39;");
        return true;
    } 
    function gc($lifetime)
    {
       $this -> db -> query("delete from `sessions` 
       where unix_timestamp(now()) - unix_timestamp(`last_visit`) > $lifetime");
        return true;
    } 
    // get sessions by user_id
    function get($user_id)
    {
        $user_id = $this -> db -> escape_string($user_id);
        return $this -> db -> query("select * from `sessions` where `user_id` = &#39;$user_id&#39;");
    } 
    // get sessions list
    function lists($page, $rows)
    {
        if ($page == 0)
        {
            return $this -> db -> query("select * from `sessions` order by `user_id`");
        } 
        else
        {
            $start = ($page - 1) * $rows;
            return $this -> db -> query("select * from `sessions` order by `user_id` limit $start, $rows");
        } 
    } 
} 

?>
登入後複製

以上就是session保存在mysql 的内容,更多相关内容请关注PHP中文网(www.php.cn)!


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板