【问题标题】:Cookie randomly expiring on Laravel 4.1Cookie 在 Laravel 4.1 上随机过期
【发布时间】:2014-01-24 08:28:35
【问题描述】:

我正在用 Laravel 4.1 做一个应用程序,当用户登录时,我正在注册一些 cookie:

Cookie::queue('adm-chave', $chave, 30);
Cookie::queue('adm-usuario', $usuario->usuario, 30);
Cookie::queue('adm-usuario-id', $usuario->id, 30);
Cookie::queue('adm-usuario-nome', $usuario->nome, 30);
Cookie::queue('adm-usuario-email', $usuario->email, 30);

我做了一个代码来更新每次页面加载的过期时间,如下所示:

Route::filter('logado', function()
{
  $chave = Cookie::get('adm-chave');
  $usuario = Cookie::get('adm-usuario');
  $id_usuario = Cookie::get('adm-usuario-id');
  $nome = Cookie::get('adm-usuario-nome');
  $email = Cookie::get('adm-usuario-email');

  if(empty($chave) || empty($id_usuario)){
    $resposta = Redirect::to('/');
    $resposta->withCookie(Cookie::forget('adm-chave'));
    $resposta->withCookie(Cookie::forget('adm-usuario'));
    $resposta->withCookie(Cookie::forget('adm-usuario-id'));
    $resposta->withCookie(Cookie::forget('adm-usuario-nome'));
    $resposta->withCookie(Cookie::forget('adm-usuario-email'));

    return $resposta;
  }

  Cookie::queue('adm-usuario', $usuario, 30);
  Cookie::queue('adm-usuario-id', $id_usuario, 30);
  Cookie::queue('adm-usuario-nome', $nome, 30);
  Cookie::queue('adm-usuario-email', $email, 30);
});

即使进行了续订,我的 cookie 也会随机过期,并且我的用户会在该 if 块上注销。

我错过了什么?

【问题讨论】:

    标签: php cookies laravel


    【解决方案1】:

    首先我们向 config/app.php 添加一个选项,如下所示(我已将其设置为 24 小时):

    /*
    |--------------------------------------------------------------------------
    | Session Files Lifetime
    |--------------------------------------------------------------------------
    |
    | Here you may specify the number of minutes after which old sessions in
    | storage will be seen as 'garbage' and cleaned up. Garbage collection may
    | occur during session start, depending on the lottery. The default is 24.
    |
    */
    
    'files_lifetime' => 24*60,
    

    然后我们创建两个文件。在 SessionServiceProvider.php 中,我们覆盖 SessionManager 寄存器以使用我们自己的 SessionManager:

    <?php namespace YourNamespace\Session;
    
    class SessionServiceProvider extends \Illuminate\Session\SessionServiceProvider {
    
        /**
         * Register the service provider.
         *
         * @return void
         */
        public function register()
        {
            $this->setupDefaultDriver();
    
            $this->registerSessionManager();
    
            $this->registerSessionDriver();
        }
    
        /**
         * Register the session manager instance.
         *
         * @return void
         */
        protected function registerSessionManager()
        {
            $this->app['session.manager'] = $this->app->share(function($app)
            {
                return new SessionManager($app);
            });
        }
    
    }
    

    在 SessionManager.php 中,我们也覆盖了 getOptions() 方法来设置 gc_maxlifetime:

    <?php namespace YourNamespace\Session;
    
    class SessionManager extends \Illuminate\Session\SessionManager {
    
        /**
         * Get the session options.
         *
         * @return array
         */
        protected function getOptions()
        {
            $config = $this->app['config']['session'];
    
            return array(
                'cookie_domain' => $config['domain'], 'cookie_lifetime' => $config['lifetime'] * 60,
                'cookie_path' => $config['path'], 'cookie_httponly' => '1', 'name' => $config['cookie'],
                'gc_divisor' => $config['lottery'][1], 'gc_probability' => $config['lottery'][0],
                'gc_maxlifetime' => $config['files_lifetime'] * 60,
            );
        }
    
    }
    

    解决方案来自https://github.com/laravel/framework/issues/2314

    【讨论】:

    • 尝试使用该方法,但没有奏效...当我在 app.php 的提供程序数组上加载这两个文件时,Laravel 抛出:ReflectionException Class session does not exist 当我做 php artisan dump-auto,它也会抛出:{"error":{"type":"ReflectionException","message":"Class session 不存在","file":"\/home\/heitor\/public_html\/ project\/vendor\/laravel\/framework\/src\/Illuminate\/Container\/Container.php","line":476}} 如何将这些文件包含到项目中以及如何在应用程序中调用它们.php?我必须更改为 Laravel 的哪些文件知道并使用这些自定义提供程序?
    • 此解决方案适用于 4.0,会话处理已在 4.1 中从头开始重建。
    猜你喜欢
    • 2015-03-12
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 2019-05-27
    • 2017-08-14
    • 1970-01-01
    相关资源
    最近更新 更多