2

php.net マニュアルのsession_set_save_handler例はファイル ベースです。私はそれをmysqlに実装しようとしました。

Windowsでユニサーバーを使用しています。エラー表示の私の設定は開発環境です。

私の問題は、ガベージコレクション機能を自動的に機能させることができなかったことです。エラー/警告はありませんでした。関連するすべてのコードは以下のとおりです。

私が自動的に意味するのは$handler->gc($maxlifetime);、セッションのテスト後に index.php で使用すると、動作して期限切れの行が削除されるということです。

index.php で使用しない$handler->gc($maxlifetime);と、期限切れの行が残ります。

私の質問

q1 - $handler->gc($maxlifetime);index.php で使用する必要がありますか?

q2 - q1 の答えが YES の場合、コールバック関数に関数がある理由gc

q3$handler->gc($maxlifetime); - q1 に対する答えが NO の場合、ブラウザ (ie11) を閉じて index.php を更新し、mysql セッション テーブルを確認した後、期限切れの行が削除されないのはなぜですか (index.php にない場合)。$maxlifetime1 (秒)であることに注意してください。

感謝をこめて

私のindex.php

// populate inclusion paths
...

// mysql PDO connect
require_once("config.php");
$dbh = new PDO("mysql:host=$my_host;dbname=$my_db;charset=utf8", $my_username, $my_password);

// start session
require_once("class_Session.php");

$maxlifetime = 1; // seconds

$handler = new MySessionHandler($dbh, $maxlifetime);

session_set_save_handler(
array($handler, 'open'),
array($handler, 'close'),
array($handler, 'read'),
array($handler, 'write'),
array($handler, 'destroy'),
array($handler, 'gc')
);

// the following prevents unexpected effects when using objects as save handlers
register_shutdown_function('session_write_close');

session_start();

$_SESSION['user'] = 'joe';

if (isset($_SESSION['user'])) { echo $_SESSION['user']; }

// note if I try code below, expired rows are deleted
// $handler->gc($maxlifetime);

// some other irrelevant-to-this case codes
...

// close mysql connection
$dbh = NULL;

MySessionHandler クラス

class MySessionHandler {

    private $_db;
    private $maxlifetime;

    function __construct(PDO $dbh, $maxlifetime) 
    {
        $this->_db = $dbh;
        $this->maxlifetime = $maxlifetime;
        return (TRUE);
    }

    function open()
    {       

    }

    function close()
    {
        $this->_db = NULL;

        return (TRUE);
    }

    function read($id)
    {
        $query = "SELECT `data` FROM `sessions` WHERE id = ? LIMIT 1";
        $this->read_stmt = $this->_db->prepare($query);
        $this->read_stmt->bindParam(1, $id, PDO::PARAM_STR);
        $this->read_stmt->execute();
        $data = $this->read_stmt->fetchColumn();
        return $data;
    }

    function write($id, $data)
    {
        $query = "REPLACE `sessions` (`id`, `data`) VALUES(?,?)";
        $this->w_stmt = $this->_db->prepare($query);
        $this->w_stmt->bindParam(1, $id, PDO::PARAM_STR);
        $this->w_stmt->bindParam(2, $data, PDO::PARAM_STR);
        $this->w_stmt->execute();

        return (TRUE);
    }

    function destroy($id)
    {
        $query = "DELETE FROM `sessions` WHERE id = ?";
        $this->delete_stmt = $this->_db->prepare($query);
        $this->delete_stmt->bindParam(1, $id, PDO::PARAM_STR);
        $this->delete_stmt->execute();

        return (TRUE);
    }

    function gc($maxlifetime)
    {
        // instead of CURRENT_TIMESTAMP(), I also tried NOW() command
        $query = "DELETE FROM `sessions` WHERE `update_time` < CURRENT_TIMESTAMP() - INTERVAL ? SECOND";
        $this->gc_stmt = $this->_db->prepare($query);
        $this->gc_stmt->bindParam(1, $this->maxlifetime, PDO::PARAM_STR);
        $this->gc_stmt->execute();
        return (TRUE);

    }
}

セッション テーブルの関連 SQL

CREATE TABLE `sessions`
(
    `id` CHAR(32) NOT NULL,
    `data` BLOB,
    `update_time` TIMESTAMP NOT NULL,
    PRIMARY KEY (id),
    INDEX (update_time)
) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_unicode_ci;
4

0 に答える 0