douxia2053 2015-02-25 22:37
浏览 22
已采纳

如何在PHP中获取特定的会话路径?

I want to save a particular session as a path. So if I delete it from MySQL database, the session would be deleted too...

Is this possible? Thank you.

  • 写回答

1条回答 默认 最新

  • douchang8219 2015-02-26 23:59
    关注

    Ok, so this is what I did... Please check if this code is correct and will be able to insert $_SESSION['email'] to db as a path...

       <?php session_set_save_handler(
            "openSession",
            "closeSession",
            "readSession",
            "writeSession",
            "destroySession",
            "gcSession"
        ); 
    
    $mysqli = new mysqli("localhost", "root", "**password**", "**table**");
    
    class Session {
        /** @var Database */
        private $mysqli;
    
    }
    
    
    function __construct()
    {
        // instance of new Database object
        $this->mysqli = CDatabase::init(); //check this
    
        // set handler to overide SESSION
        @session_set_save_handler(
            array($this, "openSession"),
            array($this, "closeSession"),
            array($this, "readSession"),
            array($this, "writeSession"),
            array($this, "destroySession"),
            array($this, "gcSession")
        );
    
        // start the session
        session_start();
    }
    
    
    public function openSession(
        // if database connection exists
        if($this->_db){
            return true;
        }
        return false;
    }
    
    
    public function closeSession()
    {
        // if database connection is closed
        if($this->mysqli->close()){
            @session_write_close();
            return true;
        }
        return false;    
    }
    
     $email = $_SESSION['email'];
    public function readSession($email)
    {
        $result = $this->mysqli->select(
            "SELECT session_data FROM sessions WHERE session_id = :session_id",
            array(":session_id"=>$email)
        );
        return isset($result[0]) ? $result[0] : "";
    }
    
    
    public function writeSession($email)
    {        
        $result = $this->mysqli->insert(
            "INSERT INTO table VALUES (session_id = :session_id)",
            array(":session_id"=>$email)
        );
        if(isset($result[0])){
            $result = $this->mysqli->update(
                "sessions",
                array(
                    "expires_at"=>time()+$this->getTimeout(),
                    "session_data"=>$data
                ),
                "session_id = :session_id",
                array(":session_id"=>$id)
            );
        }else{
            $result = $this->_db->insert(
                "sessions",
                array(
                    "session_id"=>$email,
                    "expires_at"=>time()+$this->getTimeout()
    
                )
            );
        }
    
        return true;
    }
    
    public function destroySession($email)
    {
        return $this->_db->delete(
            "sessions", "session_id = :session_id", array(":session_id"=>$email)
        );        
    }
    
    
    public function gcSession($maxLifetime)
    {
        return $this->_db->delete(
            "sessions", "expires_at < :expires_at", array(":expires_at"=>time())
        );
    }
    
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度