phalcon/incubator-session

Phalcon孵化器会话适配器

v2.0.0 2023-11-06 23:25 UTC

This package is auto-updated.

Last update: 2024-09-05 20:58:24 UTC


README

Discord Packagist Version PHP from Packagist codecov Packagist

问题跟踪器

https://github.com/phalcon/incubator/issues

数据库

此适配器使用数据库后端来存储会话数据

use Phalcon\Db\Adapter\Pdo\Mysql;
use Phalcon\Incubator\Session\Adapter\Database;

$di->set('session', function () {
    // Create a connection
    $connection = new Mysql([
        'host'     => 'localhost',
        'username' => 'root',
        'password' => 'secret',
        'dbname'   => 'test',
    ]);

    $session = new Database($connection, 'session_data');
    $session->start();

    return $session;
});

此适配器使用以下表来存储数据

 CREATE TABLE `session_data` (
  `session_id` VARCHAR(35) NOT NULL,
  `data` text NOT NULL,
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified_at` TIMESTAMP NULL DEFAULT NULL,
  PRIMARY KEY (`session_id`)
);

从phalcon/incubator 3.4升级将需要更改session_data表

ALTER TABLE session_data MODIFY COLUMN created_at TIMESTAMP DEFAULT current_timestamp() NOT NULL;
ALTER TABLE session_data MODIFY COLUMN modified_at TIMESTAMP DEFAULT NULL NULL;

Mongo

通过pecl安装PHP MongoDB扩展

pecl install mongodb

安装后,将以下行添加到您的php.ini文件中

extension=mongodb.so

此适配器使用Mongo数据库后端来存储会话数据

use Phalcon\Incubator\Session\Adapter\Mongo as MongoSession;

$di->set('session', function () {
    // Create a connection to mongo
    $mongo = new \MongoDB\Client(
        'mongodb+srv://<username>:<password>@<cluster-address>/test?retryWrites=true&w=majority'
    );

    // Passing a collection to the adapter
    $session = new MongoSession([
        'collection' => $mongo->test->session_data,
    ]);
    $session->start();

    return $session;
});