Keep session alive when close brower

Hi all, im facing with a session problem :(.

When i close browser, session is destroy. Here is my config:

‘user’ => array(

        // enable cookie-based authentication


        'enableAutoLogin' => true,


        'identityCookie' => [ // <---- here!


            'name' => '_identity',


            'httpOnly' => true,


            'domain' => '.localtest.com',


        ],


    ),

‘session’ => [

        'name' => 'PHPBACKSESSID',


        //'savePath' => __DIR__ . '/../../sessions',


        'useCookies' => true,


        //'timeout' => 60 * 60 * 24,


        'cookieParams' => array(


            'path' => '/',


            'domain' => '.localtest.com',


        ),


    ],

Im using this config to create sessions for multi-sub-domain. And it works like a charm. In login function, i set duration is 30 days. But when i close browser, session is destroy totally.

Please help me:). Thanks a lot.

$duration is Number of seconds that the user can remain in logged-in status.

Defaults to 0, meaning login till the user closes the browser or the session is manually destroyed.

If greater than 0 and $enableAutoLogin is true, cookie-based login will be supported.

Note that if $enableSession is false, this parameter will be ignored.

Read more detail in this link

My link

Thanks for your reply.:)

Finally i found out my error, i created auth_key field on user table, and used generateAuthKey method in UserIdenetity to generate a key and saved it with each user and added it on sigup function. Using configs above and its worked like a charm:).

will u please post ur code.

Sure, In userIdentity class, note that u need to extend these methods:


public function getAuthKey() {

        return $this->auth_key;

    }


    public function validateAuthKey($authKey) {

        return $this->getAuthKey() === $authKey;

    }


    /**

     * Generates "remember me" authentication key

     */

    public function generateAuthKey() {

        $this->auth_key = Yii::$app->security->generateRandomString();

        return $this->auth_key;

    }

    /**

     * @inheritdoc

     * @param type $insert

     * @return boolean

     */

    public function beforeSave($insert) {

        if (parent::beforeSave($insert)) {

            if ($this->isNewRecord) {

                $this->auth_key = Yii::$app->getSecurity()->generateRandomString();

            }

            return true;

        }

        return false;

    }



With above configs, loginByCookie() method will be run and if you set rememberMe is true, sessions will be not destroy,