2

Is it possible to configure Craft 3 to store session data in the database? On some other CMS's this can be enabled.

The reason I'm exploring this is because we're having an issue whereby PHP sessions get "locked up" due to a long-running request that forces all other PHP requests under the same session to be queued up until the prior ones complete (until the session files are written and released from their lock). This isn't ideal.

The problem is a common one amongst PHP apps that use file-based sessions. This article explains it in-depth:
https://ma.ttias.be/php-session-locking-prevent-sessions-blocking-in-requests/

I've seen this plugin which appears to support DB sessions in Craft 2, but haven't come across anything for v3.

4

To store sessions in the DB in Craft 3, you can merge the following into your config/app.php file:

return [
    'components' => [
        'session' => [
            'class' => yii\web\DbSession::class,
            'as session' => craft\behaviors\SessionBehavior::class
        ],
    ],
];

Then you need to manually create a session table in your database. Here's a mysql migration:

CREATE TABLE session
(
    id VARCHAR(255) NOT NULL PRIMARY KEY,
    uid VARCHAR(255) NOT NULL UNIQUE,
    expire INTEGER,
    data BLOB,
    dateCreated TIMESTAMP NOT NULL,
    dateUpdated TIMESTAMP NOT NULL
)

I wrote the Craft 2 DB sessions plugin referenced in the question. The Craft 3 approach is much nicer.

1

Yes it is. Yii2 provides a DB Session for it that extends the normal session and stores the values in your database instead. But you need to attach Crafts SessionBehavior and maybe make it compatible with it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.