Laravel session out the box not working

本小妞迷上赌 提交于 2019-12-13 07:04:04

问题


I've created a new laravel installation and the first thing I'm trying to do it just confirm sessions are working:

Route::get('/', function () {

    $value = Request::session()->get('key');
    if (is_null($value)) {
        Request::session()->set('key', md5(rand()));
    }
    dd(Request::session()->get('key'));

    return view('welcome');
});

I'm hoping here that session 'key' is stored, then, on the next refresh, it will output (dd) the same key value each time. However, it doesn't. I get a different value for 'key' each time. So I guess session data is not being stored?

If it helps, below is the config/session.php content:

'driver' => env('SESSION_DRIVER', 'file'),
...
'files' => storage_path('framework/sessions'),
...

My .env has the following:

...
SESSION_DRIVER=file
...

And permissions on the folders are:

$ ll /var/www/o-eco/website/storage/framework
total 24
drwxrwxr-x 1 ubuntu www-data 4096 Oct  3 02:33 ./
drwxrwxr-x 1 ubuntu www-data 4096 Oct  3 02:33 ../
drwxrwxr-x 1 ubuntu www-data 4096 Oct  3 02:33 cache/
-rw-rw-r-- 1 ubuntu www-data  103 Oct  3 02:33 .gitignore
drwxrwxr-x 1 ubuntu www-data 4096 Jan 13 10:59 sessions/
drwxrwxr-x 1 ubuntu www-data 4096 Jan 13 10:59 views/

$ ll /var/www/o-eco/website/storage/framework/sessions
total 16
drwxrwxr-x 1 ubuntu www-data 4096 Jan 13 10:59 ./
drwxrwxr-x 1 ubuntu www-data 4096 Oct  3 02:33 ../
-rw-rw-r-- 1 ubuntu www-data  258 Jan 13 10:59 5xqzOeb6f5lLKyvZIwOeonutkmluREfcaQ5owTNE
-rw-rw-r-- 1 ubuntu www-data   14 Oct  3 02:33 .gitignore

Note: this time, I'm developing within a vagrant box so all files are ubuntu:www-data. Also, I've even tried setting the sessions dir/files to 777 but still no difference.

Also, I have two cookies: XSRF-TOKEN and laravel_session. These both remain unchanged on each refresh.


回答1:


Just use save(); after put it will works

Just try this

Session::put('key', md5(rand())); 
Session::save();

EDIT

removed dd()

Route::get('/', function () {

    $value = Request::session()->get('key');
    echo $value;
    if (is_null($value)) {
        Request::session()->set('key', md5(rand()));
    }

    return view('welcome');
});


来源:https://stackoverflow.com/questions/41633766/laravel-session-out-the-box-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!