【问题标题】:Laravel 4 session won't persist across page loadsLaravel 4 会话不会在页面加载时持续存在
【发布时间】:2014-01-28 14:32:15
【问题描述】:

我想将oauth2 server 集成到我的 laravel 4.1 项目中。密码流程很顺利,但是在编写授权代码流程时,我遇到了一些奇怪的会话问题。

生成请求会导致此功能被过滤为登录用户

Route::get('security/authorize', array('before' => 'check-authorization-params|auth', function()
{
    [...]
}));

访客被重定向到表单以登录

Route::get('security/login', array('before' => 'guest' ,function()
{
    return View::make('users.login');
}));

Wich 导致用户应该登录并重定向到他打算执行的请求的这条路线:

public function login()
{
    $creds = array(
        'email' => Input::get('email'),
        'password'=>Input::get('password')
    );

    if(Auth::attempt($creds, true))
    {
        return Redirect::intended('/');
    }

    return Redirect::to('security/login');
}

问题是,尽管 Auth::attempt() 是肯定的,但我仍然被 auth 过滤器重定向。 我做了一些简短的测试,通过设置和读取从未达到下一个请求的会话数据来检查出了什么问题,所以我发现我必须与我的会话有关。

这是我的会话配置文件

<?php
return array(
'driver' => 'database',
    'lifetime' => 120,
    'expire_on_close' => false,
    'files' => storage_path().'/sessions',
    'connection' => 'mysql',
    'table' => 'sessions',
    'lottery' => array(2, 100),
'cookie' => 'laravel_session',
'path' => '/',
'domain' => null,
);

以下是我仔细检查过的一些事情:

  • 数据库连接正确,会话显示在我的表中
  • 会话表字段具有正确的数据类型(id - varchar(255)、payload - text、last_activity - int(11))
  • cookie 被设置

【问题讨论】:

  • 这似乎是一个基于问题的奇怪问题......但你有机会使用 Chrome 吗?
  • Redirect::intended('/'); Redirect::intended() 函数采用默认参数回退到以防会话中没有 url.intended
  • 我在玩 safari,但我在 Chrome、Firefox 甚至 IE 上测试过
  • @AyobamiOpeyemi 你说得对,我应该设置回退,但这不是导致我的会话出现问题的原因
  • 您是否被重定向到“/security/login”?

标签: php session cookies laravel-4


【解决方案1】:

事实证明,我没有在我的用户模型中正确设置身份验证标识符。 从这里改变它

public function getAuthIdentifier()
{
    return $this->email;
}

到这里

public function getAuthIdentifier()
{
    return $this->id;
}

修复一切。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    相关资源
    最近更新 更多