簡體   English   中英

Session_set_save_handler未寫入數據庫

[英]Session_set_save_handler not writing to database

我目前正在學習php,並嘗試將會話數據寫入數據庫而沒有成功。 我有一個Apache24,PHP 7環境和Postgresql數據庫的安裝程序。 當我在其他PHP文件中實例化sessionhandling類( $sess = new sessionhandling )時,沒有任何內容寫入數據庫。 但是,當我將變量傳遞給並調用寫入函數( $sess->write )時,數據將寫入數據庫。

(希望這不是其他問題的重復。在Stackoverflow和Google上做了很多搜索,但沒有找到解決我難題的答案)

我的會話處理程序代碼如下:

    <?php
    Include(dirname(__DIR__).'\Userstories\db\Connection.php');
    class sessionhandling extends Connecting implements SessionHandlerInterface {
        public function __construct(){

            // Set handler to overide SESSION

            session_set_save_handler(

                array(&$this, "open"),
                array(&$this, "close"),
                array(&$this, "read"),
                array(&$this, "write"),
                array(&$this, "destroy"),
                array(&$this, "gc")
                );

            register_shutdown_function('session_write_close');

            // Start the session
            session_start();
            session_write_close;

            }

        public function open($save_path, $id) {
            if(self::get()->connect()) {
                return true;
            } else {
                return false;
            }
        }

        public function close() {
            if(self::get()->connect()->pdo = Null) {
                return true;
            } else {
                return false;
            }
        }       

        public function read($id) {
            //$pdo = Connecting::get()->connect();
            $ipdo = self::get()->connect();
            $q_udata = "SELECT data FROM sessions WHERE id=:id";
            $stmt=$ipdo->prepare($q_udata);
            $stmt->bindvalue(':id', $id);
            $stmt->execute();

            if($stmt->execute()) {
                $row = $stmt->fetch(PDO::FETCH_ASSOC);
                $ipdo = NULL;
                return $row['data'];
            } else {
                $ipdo = NULL;
                return '';
            }

        }

        public function write($id, $data){
            $id = (string) $id;
            $data = (string) $data;
            $access = time();
            $ipdo = self::get()->connect();
            $c_id = "SELECT id FROM sessions WHERE id=:id";
            $stmt=$ipdo->prepare($c_id);
            $stmt->bindvalue(':id', $id);
            $stmt->execute();
            $idarray=$stmt->fetch(PDO::FETCH_ASSOC);
            $row_id = $idarray['id'];   

            if(empty($row_id)) {
                $sessionids = 'INSERT INTO sessions(id, data, access) VALUES(:id, :data, :access)';
                $stmt = $ipdo->prepare($sessionids);
                $stmt->bindvalue(':id', $id);
                $stmt->bindvalue(':access', $access);
                $stmt->bindvalue(':data', $data);
                $stmt->execute();
                session_write_close();
            } else {

                $rep_data = "UPDATE sessions SET data = :data, access = :access WHERE id = :id";
                $stmt=$ipdo->prepare($rep_data);
                $stmt->bindvalue(':id', $id);
                $stmt->bindvalue(':access', $access);
                $stmt->bindvalue(':data', $data);
                $stmt->execute();
                session_write_close();
            }

            if($stmt->execute()) {
                $ipdo = NULL;
                return true;
            } else {
                $ipdo = NULL;
                return false;
            }
        }

        public function destroy($id) {
            $ipdo = self::get()->connect();
            $del_data = "DELETE FROM sessions WHERE id =:id";
            $stmt = $ipdo->prepare($del_data);
            $stmt->bindvalue(':id', $id);
            $stmt->execute();
            if($stmt->execute()) {
                $ipdo = NULL;
                return true;
            } else {
                $ipdo = NULL;
                return false;
            }
        }

        public function gc($max) {
            $old = time() - $max;

             $ipdo = self::get()->connect();
             $cleanup = "DELETE * FROM sessions WHERE access < :old";
             $stmt = $ipdo->prepare($cleanup);
             $stmt->bindvalue(':old', $old);
             $stmt->execute();
             if($stmt->execute()) {
                $ipdo = NULL;
                return true;
            } else {
                $ipdo = NULL;
                return false;
            }
        }


    }

?>

當我從implements SessionHandlerInterface函數中刪除“ implements SessionHandlerInterface ”會話處理類並刪除參數$ save_path,$ id時,出現以下錯誤:“警告:session_start():無法讀取會話數據:C語言中的用戶(路徑:):第19行的\\ Users \\ Public \\ Server \\ Apache24 \\ htdocs \\ Userstories \\ sessionhandling.php”

使用DB進行會話處理時是否需要定義$ save_path? 如果是這樣,$ save_path應該是什么?

非常感謝有關如何使我的會話處理程序寫入數據庫的任何建議。

我通過將read函數更改為此並確保返回字符串來完成ut工作:

public function read($id) {
    //$pdo = Connecting::get()->connect();
    $ipdo = self::get()->connect();
    $q_udata = "SELECT data FROM sessions WHERE id=:id";
    $stmt=$ipdo->prepare($q_udata);
    $stmt->bindvalue(':id', $id);
    $stmt->execute();

    if($stmt->execute()) {
        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
        $ipdo = NULL;
        $data = $row['data'];
        return (string) $data;
    } else {
        $ipdo = NULL;
        return '';
    }

}

我知道其他文章已經指出了這一點,但是我認為我的$data = $row['data']首先會返回一個字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM