Laravel sessions regenerating on every load

后端 未结 8 2277
盖世英雄少女心
盖世英雄少女心 2021-02-18 19:05

I\'m having a lot of problems and I really can\'t seem to find a solution.

Yesterday, I finished up doing some work on a vagrant box in Laravel. When I shut down the com

相关标签:
8条回答
  • 2021-02-18 19:25

    I also has this problem, and it turned out to be with outputting data early - I was already echoing some debug text in the controller action, and then tried setting a session variable for the first time - the session just wouldn't persist, and kept recreating each time - once I removed the output, it worked fine (and fine from then onwards)

    0 讨论(0)
  • 2021-02-18 19:27

    I upgraded from Laravel 5.1 to 5.2 and had the issue of numerous sessions being created on every page load. It didn't matter which session driver I used, nor did changing anything in config make it work. (I tried every suggestion on every StackOverflow result produced by Google.)

    The solution for me: remove "web" middleware from your routes. Apparently, web middleware is included automatically in 5.2+, so if your routes ALSO specify it, you wind up with multiple sessions being generated.

    0 讨论(0)
  • 2021-02-18 19:31

    My problem was that I had echo statements in my function. It was keeping the session from being properly created/stored.

    I had an echo before my 'return Redirect' so the redirect didn't get the session...

    public function login()
    {
        // auth
        if (Auth::attempt(Input::only('email', 'password'), true))
        {
            return Redirect::route('companies.index');
        }
        // failed -> back to login
        return Redirect::back()->withErrors(['email' => 'Login failed.'])->withInput();
    }
    

    Details: http://brainwashinc.com/2014/02/17/laravel-sessions-not-working-in-4-1/

    0 讨论(0)
  • 2021-02-18 19:41

    I spent a whole day with the same issue (session regenerated with almost every request), and could not find anything helpful online. Posting this here in case somebody has the same issue.

    Basically the problem was that the session cookie was not set correctly due to template errors. So if you run into this, first check that the session cookie is set with every request via http headers.

    In my case I wrongly used something like this in some of my templates:

    @section('test')
    <p>Some content here</p>
    @endsection
    

    This is wrong!

    In Laravel 4 it needs to be @stop instead of @endsection:

    @section('test')
    <p>Some content here</p>
    @stop
    

    Using @endsection made the app not finish correctly, so no session cookie was set. There is no trace of anything being wrong in the logfiles however. BTW, this kind of error also leads to after-filters not being applied.

    0 讨论(0)
  • 2021-02-18 19:43

    I had this same issue and already changed my session values to the 4,1 version. I logged into our server and ran the composer update command, after it finished everything worked fine.

    composer update
    
    0 讨论(0)
  • 2021-02-18 19:46

    There are many reasons this can happen, I just ran into this problem myself. The fix for me was to change:

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

    to

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

    on the User model.

    0 讨论(0)
提交回复
热议问题