duangu8264 2018-02-09 11:09
浏览 57

Session_set_save_handler没有写入数据库

I'm currently learning php and trying to write session data to my database without success. I have a setup with Apache24, PHP 7 environment and Postgresql database. When I instantiate sessionhandling class ($sess = new sessionhandling) in my other PHP file nothing is written to database. However, when I pass variable to and call the write function ($sess->write), data is written to the database.

(Hope this is not a duplicate of any other questions raised. Done a lot of searches on Stackoverflow and Google, but not found any answers that solve my challenge)

My session handler code is as follows:

    <?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;
            }
        }


    }

?>

When I remove the 'implements SessionHandlerInterface' sessionhandling class and remove the parameters $save_path, $id from open function, I get the following error: "Warning: session_start(): Failed to read session data: user (path: ) in C:\Users\Public\Server\Apache24\htdocs\Userstories\sessionhandling.php on line 19"

Is it reuiqred to define the $save_path when using DB for session handling? If so, what should the $save_path be?

Any advise on how to get my session handler to write to DB is very much appreciated.

  • 写回答

1条回答 默认 最新

  • dtjwov4984 2018-02-09 19:38
    关注

    I made ut work by changing my read function to this and ensuring that a string is returned:

    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 '';
        }
    
    }
    

    I know this has been pointed out in other posts, but I thought that my $data = $row['data'] would return a string in the first place.

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?