用户登录后Laravel 5会话未持久 [英] Laravel 5 session not persisting after user is logged in

查看:70
本文介绍了用户登录后Laravel 5会话未持久的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel 5中遇到了一个有趣的问题.

I'm having an interesting issue with Laravel 5.

登录用户后,登录状态不会在页面之间保持不变.显然,它与Session::有关.

After logging in a user, the logged in status is not persisted across pages. Clearly it has something to do with Session::.

我登录用户的方式非常简单:

The way I'm logging in a user is pretty straight-forward:

if (Auth::attempt(['email' => $data['email'], 'password' => $data['password']],
    isset($data['remember_me']) ? TRUE : FALSE))
{
    return redirect()->intended('/');
}

如果用户未登录,则简单的print_r(Session::all());会给我以下信息:

A simple print_r(Session::all()); gives me the following if the user is NOT logged in:

Array
(
    [_token] => wV8o75lZnCZ0f6CMMQgdBBM2AxSYjtWisAXx6TgZ
    [flash] => Array
        (
            [old] => Array
                (
                )

            [new] => Array
                (
                )

        )

    [_previous] => Array
        (
            [url] => http://localhost/public
        )

)

在用户登录到重定向到/后,该数组如下所示:

After the user is logged in an redirected to / the array looks like this:

Array
(
    [_token] => wV8o75lZnCZ0f6CMMQgdBBM2AxSYjtWisAXx6TgZ
    [flash] => Array
        (
            [old] => Array
                (
                )

            [new] => Array
                (
                )

        )

    [_previous] => Array
        (
            [url] => http://localhost/public/
        )

    [login_82e5d2c56bdd0811318f0cf078b78bfc] => 2
)

但是,在执行任何导致页面刷新或重定向的操作之后,会话状态都会丢失.

However, after any action that will lead to a page refresh or a redirect, the session status is lost.

我的config/session.php文件如下所示:

<?php

return [
    'driver' => env('SESSION_DRIVER', 'file'),
    'lifetime' => 120,
    'expire_on_close' => false,
    'encrypt' => false,
    'files' => storage_path('framework/sessions'),
    'connection' => null,
    'table' => 'sessions',
    'lottery' => [2, 100],
    'cookie' => 'laravel_session',
    'path' => '/',
    'domain' => null,
    'secure' => false,

];

可以写入和读取会话的本地存储文件.

The locally stored file for the session can be written and read.

我尝试使用database驱动器而不是文件. [login_xx] => 2键/值丢失,我也注销了.

I've tried using database drive instead of file. Same thing happens the [login_xx] => 2 key/value is lost and I'm logged out.

由于Session::尚未完全重置,所以我怀疑我没有正确登录用户,或者只是在执行我不应该在某处执行的操作.

Since the Session:: is not completely reset I'm suspecting that I'm not logging in the user properly or simply doing something that I shouldn't be doing somewhere.

推荐答案

我遇到了类似的问题,我简称为:

I faced similar issue, I simply called:

Session::save();

在对会话存储进行任何添加/更新/删除之后.看起来像这样:

after any add/update/delete to Session storage. So it looked like:

$id = Input::get('id');
Session::forget('cart.' .$id);
Session::save();

这篇关于用户登录后Laravel 5会话未持久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆