2017-12-18 16:20:10 5684次浏览 3条回答 0 悬赏 30 金钱
Yii::$app->user->login($this->getUser(),  0);

我用如上方法登陆, 以后看到浏览器产生一个PHPSESSION的cookie, 有效期是会话, 我理解是关闭浏览器以后, 应该就失效。 但是实际情况是关闭浏览器下次打开, PHPSESSION还在, 直接给自动登陆了!
怎么样才能保证浏览器关闭时候失效, 包括一些意外的浏览器奔溃, 或者是电脑直接断电的情况。

补充于 2017-12-28 14:48
<?php

$params = require(__DIR__ . '/params.php');
$db = require(__DIR__ . '/db.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'language' => 'zh-CN',
    'components' => [
        'assetManager' => [
            'assetMap' => [
               // 'jquery.js' => '@web/js/jquery-1.11.1.min.js',
            ],
//            'bundles' => [
//                'yii\web\JqueryAsset' => [
//                    'sourcePath' => null,
//                    'js' => []
//                ],
//            ],
        ],
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
        ],
        'formatter' => [
            'class' => 'yii\i18n\Formatter',
            'nullDisplay' => '',

        ],
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'IdMsnYBmQFgfd9fz4m3vh_BOx61mfPWUZ',
            'enableCookieValidation' => false,
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],

        'user' => [
            'identityClass' => 'app\models\Operator',
            'enableAutoLogin' => false,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['info'],//这里说明配置的是那个,如error,warning,info,trace这些
                    'categories' => ['info'],//这里说明的保存的文件名
                    'logFile' => '@app/runtime/logs/info.log',//这里说明的保存的文件名的路径
                    'maxFileSize' => 1024 * 2,//这些一看就知道的就不说明了
                    'maxLogFiles' => 20,
                ],
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning', 'info'],
                ],

            ],
        ],
        'db' => $db['db1'],


        'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => false,  //不启用严格解析
            'showScriptName' => false, //隐藏index.php
        	'suffix'=>'.jsp',
            'rules' => [
//                '<module:\w+>/<controller:\w+>/<id:\d+>' => '<module>/<controller>/view',
//                '<controller:\w+>/<id:\d+>' => '<controller>/view',
              	'<controller:(post|comment)>s'=>'<controller>/index',
            	'<controller:\w+>/<id:\d+>'=>'<controller>/view',
            	'<controller:\w+>/<id:\d+>/<action:(create|update|delete)>'=>'<controller>/<action>',
            ],
        ],

    ],
    'params' => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
//    $config['bootstrap'][] = 'debug';
//    $config['modules']['debug'] = [
//        'class' => 'yii\debug\Module',
//        // uncomment the following to add your IP if you are not connecting from localhost.
//        'allowedIPs' => ['127.0.0.1', '::1'],
//    ];

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        //'allowedIPs' => ['127.0.0.1', '::1'],
    ];
}

return $config;

这是我的配置文件

补充于 2018-01-11 09:15

最后发现问题了, 是我的浏览器配置"启动时, 显示上次网页", 所以关闭浏览器时没有清楚cookie

最佳答案

  • 小叮当的肚兜 发布于 2017-12-20 19:23 举报

    cookie可以设置退出就失效 仔细了解一下原理就好了.

    1 条回复
    回复于 2017-12-20 22:46 回复

    怎么设置退出失效? 我看了下网上资料说不设置就是默认退出关闭, 我现在就是没有设置。

  • 回答于 2017-12-21 19:09 举报

    2.png
    这个配置上写了

  • 回答于 2017-12-26 08:54 举报

    把设置中的'enableAutoLogin' => true去掉即可

    2 条回复
    回复于 2017-12-26 10:26 回复
            'enableAutoLogin' => false,我都配置成false了
    
    回复于 2017-12-26 13:26 回复

    这样设置以后,按照系统自带的代码,有效期已经是会话的 了。 除非你自己有另外的代码去处理cookie,搞成自动登录了

您需要登录后才可以回答。登录 | 立即注册
秀小川
主管

秀小川

注册时间:2017-08-16
最后登录:2022-05-23
在线时长:20小时47分
  • 粉丝1
  • 金钱455
  • 威望0
  • 积分655

热门问题