Laravel Redis Session Driver doesn't work

落花浮王杯 提交于 2020-12-05 08:32:13

问题


I'm having difficulties using the laravel 4 "redis" session driver. The problem is nothing gets actually stored.

Things i've checked:

  • I've set the driver to redis in Session-config file and cache-config file
  • In database-config file a database config for redis is specified:
  • I tried storing something manually with the redis-class, that did work, storing with the session class did not work

    // this is in my session config
    'driver' => 'redis',
    
    //this is my db-config
    'redis' => array(
    
        'cluster' => true,
    
        'default' => array(
            'host'     => '127.0.0.1',
            'port'     => 6379,
            'database' => 0,
        ),
    
    ),
    
    
    //this works
    $redis = Redis::connection();
    $redis->set('name', 'Taylor');
    
    //this doesn't work
    Session::put('name', 'Taylor');
    

When i change the session to native it works

Any help would be appreciated

UPDATE
That's interesting: When I use redis-cli monitoras suggested by @philo, I get some output while trying to log in my L4 application:

`1387191809.513730 [0 127.0.0.1:59268] "SELECT" "0"
1387191809.513835 [0 127.0.0.1:59268] "GET" "laravel:siau639prmckja34le11vbsfl7"
1387191809.863851 [0 127.0.0.1:59268] "SET" "laravel:siau639prmckja34le11vbsfl7" "s:226:\"_sf2_attributes|a:2:{s:6:\"_token\";s:40:\"9LPf354C2ZNtw0Oc1zyafSvMdFFlspPiJsq8w90v\";s:5:\"flash\";a:2:{s:3:\"old\";a:0:{}s:3:\"new\";a:0:{}}}_sf2_flashes|a:0:{}_sf2_meta|a:3:{s:1:\"u\";i:1387191809;s:1:\"c\";i:1387191809;s:1:\"l\";s:1:\"0\";}\";"
1387191809.866655 [0 127.0.0.1:59268] "EXPIRE" "laravel:siau639prmckja34le11vbsfl7" "0"
1387191828.464840 [0 127.0.0.1:59279] "SELECT" "0"
1387191828.464970 [0 127.0.0.1:59279] "GET" "laravel:siau639prmckja34le11vbsfl7"
1387191828.581774 [0 127.0.0.1:59279] "SET" "laravel:siau639prmckja34le11vbsfl7" "s:599:\"_sf2_attributes|a:4:{s:6:\"_token\";s:40:\"KSkc5OAsp9Psz3MC7dYo6FfkTvcdK6I6HcisSyJ3\";s:10:\"_old_input\";a:3:{s:6:\"_token\";s:40:\"9LPf354C2ZNtw0Oc1zyafSvMdFFlspPiJsq8w90v\";s:8:\"username\";s:10:\"bertcasier\";s:8:\"password\";s:4:\"test\";}s:5:\"flash\";a:2:{s:3:\"new\";a:0:{}s:3:\"old\";a:2:{i:0;s:10:\"_old_input\";i:1;s:6:\"errors\";}}s:6:\"errors\";O:29:\"Illuminate\\Support\\MessageBag\":2:{s:11:\"\x00*\x00messages\";a:1:{s:7:\"general\";a:1:{i:0;s:48:\"Ongeldige gebruikersnaam/wachtwoordcombinatie...\";}}s:9:\"\x00*\x00format\";s:8:\":message\";}}_sf2_flashes|a:0:{}_sf2_meta|a:3:{s:1:\"u\";i:1387191828;s:1:\"c\";i:1387191828;s:1:\"l\";s:1:\"0\";}\";"
1387191828.582214 [0 127.0.0.1:59279] "EXPIRE" "laravel:siau639prmckja34le11vbsfl7" "0"
1387191828.613536 [0 127.0.0.1:59284] "SELECT" "0"
1387191828.613649 [0 127.0.0.1:59284] "GET" "laravel:siau639prmckja34le11vbsfl7"
1387191828.653734 [0 127.0.0.1:59284] "SET" "laravel:siau639prmckja34le11vbsfl7" "s:226:\"_sf2_attributes|a:2:{s:6:\"_token\";s:40:\"SMxXljPLDaViVVSpCohfOKlpByhjp8E2ywS6zVkh\";s:5:\"flash\";a:2:{s:3:\"old\";a:0:{}s:3:\"new\";a:0:{}}}_sf2_flashes|a:0:{}_sf2_meta|a:3:{s:1:\"u\";i:1387191828;s:1:\"c\";i:1387191828;s:1:\"l\";s:1:\"0\";}\";"
1387191828.654102 [0 127.0.0.1:59284] "EXPIRE" "laravel:siau639prmckja34le11vbsfl7" "0"
1387191878.208814 [0 127.0.0.1:59358] "config" "get" "databases"
1387191878.215094 [0 127.0.0.1:59358] "INFO" "keyspace"
1387191880.176125 [0 127.0.0.1:59358] "select" "0"
1387191880.176314 [0 127.0.0.1:59358] "keys" "*"`

Seems like my session expires immediately? I wrote a custom Auth User Provider which logs me in, in a rest-API, and stores the result in the session. But when I use the redis session driver, I get redirected immediately back to the login page. When I use RDM (redis client GUI) I can't find any session variables


回答1:


I've found the solution thanks to the tip from @philo.

The session lifetime in my config file was set to zero, because I want my session to expire on browser close. That works for native sessions, but for redis sessions it causes the session to expire immediately. When I change the life time, my session works as expected.

Now I'm still searching how to expire on browser close




回答2:


Now I'm still searching how to expire on browser close

Note that the lifetime of the cookie and the session data are unrelated. You want the cookie lifetime to be 0 (remove on browser close) and the session data lifetime to be longer (as long as you allow between requests).




回答3:


You have to enclose your route to controller with middlewareGroups - Laravel 5.2

Route::group(['middlewareGroups' => ['web']], function () { ... });



来源:https://stackoverflow.com/questions/20375089/laravel-redis-session-driver-doesnt-work

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