Home > Blockchain >  Where is session files stored in Yii2?
Where is session files stored in Yii2?

Time:04-03

Where is session files stored in Yii2? I need to know the the exact location. There is options to create a session database.

CodePudding user response:

The default session save path is '/tmp'. link
This path is accessible via the getSavePath() method in the session class file(yii2)

  • @property string $savePath The current session save path, defaults to '/tmp'.

For example, in xampp software (localhost) go to the following folder(default)

myDrive:\xampp\tmp  // The drive where the software is installed

It is taken by default through the session_save_path method. Which depends on the settings of the php.ini file. In session.save_path="...\tmp" But you can also configure it through the .htaccess file

To adjust Yii2, you can do the following. In the config web file

'components' => [
    'session' => [
        'name' => '_Session',
        'savePath' => dirname(__DIR__) .'/sessions'
    ],

To save in the database(yii\web\DbSession) refer to this link.
Example:

    'session' => [
        'class' => 'yii\web\DbSession',
        'name' => 'mySession',
        // 'db' => 'mydb',  // the application component ID of the DB connection. Defaults to 'db'.
        // 'sessionTable' => 'my_session', // session table name. Defaults to 'session'.
        'timeout' => 30 * 24 * 3600,
        'cookieParams' => ['httponly' => true, 'lifetime' => 3600 * 24],
        'writeCallback' => function ($session) {
            return [
                // 'user_id' => Yii::$app->user->id,
                // 'last_write' => time(),
            ];
        },
    ],

writeCallback: To create more data and columns in the database table

Good luck

CodePudding user response:

Yii2 by default stores session files in @app/runtime/data folder.

And if you want to use database instead then yii2 guide is great resource. check this link: https://www.yiiframework.com/doc/guide/2.0/en/runtime-sessions-cookies#custom-session-storage.

  • Related