Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 会话启动时没有锁定?还是解决办法?_Php_Session - Fatal编程技术网

Php 会话启动时没有锁定?还是解决办法?

Php 会话启动时没有锁定?还是解决办法?,php,session,Php,Session,我已经做了一些关于会话启动的研究,以及为什么它会锁定我的所有其他ajax请求。 我发现在执行完页面或调用session_write_close()之前,用户的会话一直处于锁定状态 使用session\u set\u save\u处理程序,您可以为session设置自己的处理程序,但我不知道它是如何工作的,也不知道如何锁定session而只读取它 有人有会话设置保存处理程序的例子吗? 或者对于只读会话/解锁会话还有其他解决方法吗?使用数据库保存PHP会话可以避免锁定ajax请求。会话设置保存处理程

我已经做了一些关于会话启动的研究,以及为什么它会锁定我的所有其他ajax请求。 我发现在执行完页面或调用session_write_close()之前,用户的会话一直处于锁定状态

使用session\u set\u save\u处理程序,您可以为session设置自己的处理程序,但我不知道它是如何工作的,也不知道如何锁定session而只读取它

有人有会话设置保存处理程序的例子吗?
或者对于只读会话/解锁会话还有其他解决方法吗?

使用数据库保存PHP会话可以避免锁定ajax请求。会话设置保存处理程序允许这样做

您将需要创建一个具有类似以下内容的open、close、read、write、destroy和gc方法的类:

        /**
         * DBSession
         */
        class DBSession
        {

            /**
             * Db Object
             */
            private $db;

            public function __construct(){

                 $this->db = new DB; //initialise in your database connection object
            }

            /**
             * Open
             */
            public function open()
            {
                // If successful
                if ($this->db) {
                    // Return True
                    return true;
                }
                // Return False
                return false;
            }

            /**
             * Close
             */
            public function close()
            {
                // Close the database connection
                // If successful
                if ($this->db->close()) {
                    // Return True
                    return true;
                }
                // Return False
                return false;
            }

            /**
             * Read
             */
            public function read($id)
            {
                // Set query
                $this->db->query('SELECT data FROM sessions WHERE id = :id');

                // Bind the Id
                $this->db->bind(':id', $id);

                // Attempt execution
                // If successful
                if ($this->db->execute()) {
                    // Save returned row
                    $row = $this->db->single();
                    // Return the data
                    return $row['data'];
                } else {
                    // Return an empty string
                    return '';
                }
            }

            /**
             * Write
             */
            public function write($id, $data)
            {
                // Create time stamp
                $access = time();

                // Set query  
                $this->db->query('REPLACE INTO sessions VALUES (:id, :access, :data)');

                // Bind data
                $this->db->bind(':id', $id);
                $this->db->bind(':access', $access);
                $this->db->bind(':data', $data);

                // Attempt Execution
                // If successful
                if ($this->db->execute()) {
                    // Return True
                    return true;
                }

                // Return False
                return false;
            }

            /**
             * Destroy
             */
            public function destroy($id)
            {
                // Set query
                $this->db->query('DELETE FROM sessions WHERE id = :id');

                // Bind data
                $this->db->bind(':id', $id);

                // Attempt execution
                // If successful
                if ($this->db->execute()) {
                    // Return True
                    return true;
                }

                // Return False
                return false;
            }

            /**
             * Garbage Collection
             */
            public function gc($max)
            {
                // Calculate what is to be deemed old
                $old = time() - $max;

                // Set query
                $this->db->query('DELETE * FROM sessions WHERE access < :old');

                // Bind data
                $this->db->bind(':old', $old);

                // Attempt execution
                if ($this->db->execute()) {
                    // Return True
                    return true;
                }

                // Return False
                return false;
            }
        }
或者,您可以将会话_set _save _处理程序调用和会话_start()移动到DBSession类的构造函数中。在这种情况下,您只需在脚本的开头启动一个DBSession实例

实际上,PHP手册已经有了一些例子:

请发布您迄今为止尝试过的内容
$dbSession = new DBSession();

session_set_save_handler(
  array($dbSession, "open"),
  array($dbSession, "close"),
  array($dbSession, "read"),
  array($dbSession, "write"),
  array($dbSession, "destroy"),
  array($dbSession, "gc")
);

session_start();