Answers.Body: <p>To store sessions in the DB in Craft 3, you can merge the following into your <code>config/app.php</code> file:</p>
  
  <pre><code>return [
      'components' =&gt; [
          'session' =&gt; [
              'class' =&gt; yii\web\DbSession::class,
              'as session' =&gt; craft\behaviors\SessionBehavior::class
          ],
      ],
  ];
  </code></pre>
  
  <p>Then you need to manually create a <code>session</code> table in your database. Here's a mysql migration:</p>
  
  <pre><code>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
  )
  </code></pre>
  
  <p>I wrote the Craft 2 DB sessions plugin referenced in the question. The Craft 3 approach is much nicer.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-15T22:28:45.603Z
Answers.Text: I think table name is supposed to bve "phpsessions" now.
Answers.UserDisplayName: Piotr Pogorzelski
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-03T18:57:07.463Z
Answers.LastActivityDate: 2019-10-03T19:14:27.760Z
Answers.LastEditDate: 2019-10-03T19:14:27.760Z
Answers.UserDisplayName: Jeff T
Answers.Body: <p>Yes it is. Yii2 provides a <a href="https://www.yiiframework.com/doc/api/2.0/yii-web-dbsession" rel="nofollow noreferrer">DB Session</a> for it that extends the normal session and stores the values in your database instead. But you need to attach Crafts <a href="https://github.com/craftcms/cms/blob/develop/src/behaviors/SessionBehavior.php" rel="nofollow noreferrer">SessionBehavior</a> and maybe make it compatible with it </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-18T06:23:01.190Z
Answers.LastActivityDate: 2019-03-18T06:30:30.567Z
Answers.LastEditDate: 2019-03-18T06:30:30.567Z
Answers.UserDisplayName: Robin Schambach
Answers.Body: <p>In Craft 3.4, you can run the <code>craft setup/php-session-table</code> console command.</p>
  
  <p>That will create a <code>phpsessions</code> table in the database with the proper schema for MySQL or PostgreSQL.</p>
  
  <p>Then from your <code>config/app.php</code> file, you'll just need to set:</p>
  
  <pre class="lang-php prettyprint-override"><code>return [
      'components' =&gt; [
          'session' =&gt; [
              'class' =&gt; yii\web\DbSession::class,
              'as session' =&gt; craft\behaviors\SessionBehavior::class,
              'sessionTable' =&gt; '{{%phpsessions}}'
          ],
      ],
  ];
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-13T22:30:10.933Z
Answers.LastActivityDate: 2020-03-09T03:47:50.813Z
Answers.LastEditDate: 2020-03-09T03:47:50.813Z
Answers.UserDisplayName: Brad Bell
Body: <p><strong>Is it possible to configure Craft 3 to store session data in the database?</strong> On some other CMS's this can be enabled.</p>
  
  <p>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.</p>
  
  <p>The problem is a common one amongst PHP apps that use file-based sessions. This article explains it in-depth:<br>
  <a href="https://ma.ttias.be/php-session-locking-prevent-sessions-blocking-in-requests/" rel="nofollow noreferrer">https://ma.ttias.be/php-session-locking-prevent-sessions-blocking-in-requests/</a></p>
  
  <p>I've seen <a href="https://github.com/imarc/craft-dbsessions" rel="nofollow noreferrer">this plugin</a> which appears to support DB sessions in Craft 2, but haven't come across anything for v3.</p>
  
Tags: <session>
Title: How can I store Craft sessions in the database?
ThreadText: # How can I store Craft sessions in the database?
  
  (Asked by: username_0 on 2019-03-18)
  
  **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](https://github.com/imarc/craft-dbsessions) which appears to support DB sessions in Craft 2, but haven't come across anything for v3.
  
  **Tags:** session
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_4 on 2020-01-13. Score: 4 | Accepted answer
  
  In Craft 3.4, you can run the `craft setup/php-session-table` console command.
  
  That will create a `phpsessions` table in the database with the proper schema for MySQL or PostgreSQL.
  
  Then from your `config/app.php` file, you'll just need to set:
  
  ```php
  return [
      'components' => [
          'session' => [
              'class' => yii\web\DbSession::class,
              'as session' => craft\behaviors\SessionBehavior::class,
              'sessionTable' => '{{%phpsessions}}'
          ],
      ],
  ];
  
  ```
  
  
  ---
  
  ### Answer by username_1 on 2019-10-03. Score: 7
  
  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.
  
  > **Comments:**
  > **username_2**: I think table name is supposed to bve "phpsessions" now.  
  
  ---
  
  ### Answer by username_3 on 2019-03-18. Score: 1
  
  Yes it is. Yii2 provides a [DB Session](https://www.yiiframework.com/doc/api/2.0/yii-web-dbsession) for it that extends the normal session and stores the values in your database instead. But you need to attach Crafts [SessionBehavior](https://github.com/craftcms/cms/blob/develop/src/behaviors/SessionBehavior.php) and maybe make it compatible with it
  
  
  ---
  
  