【问题标题】:Session_set_save_handler not writing to databaseSession_set_save_handler 未写入数据库
【发布时间】:2018-02-09 11:09:04
【问题描述】:

我目前正在学习 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' 会话处理类并从打开函数中删除参数 $save_path、$id 时,我收到以下错误:“警告:session_start():读取会话数据失败:用户(路径: ) 在 C:\Users\Public\Server\Apache24\htdocs\Userstories\sessionhandling.php 第 19 行"

在使用 DB 进行会话处理时是否需要定义 $save_path?如果是,$save_path 应该是什么?

非常感谢任何有关如何让我的会话处理程序写入 DB 的建议。

【问题讨论】:

    标签: php constructor session-set-save-handler


    【解决方案1】:

    我通过将我的读取函数更改为此并确保返回一个字符串来完成工作:

    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'] 首先会返回一个字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-16
      • 2011-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多