Laravel sessions regenerating on every load

£可爱£侵袭症+ 提交于 2019-12-09 14:30:47

问题


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 computer, login functionality was working fine.

On opening everything up today I find sessions are no longer working.

I've tried everything I can think of but I cannot log in now and flash data doesn't work.

Every page load a new session is created - e.g using the native driver, if I login, it creates two sessions - one for the login page, and one for the posted login page.

If I use a database driver I get the same result. Every time I click login, I get a further two rows of session data

I can't tell what's happening with the cookie driver but I've tried it and it's not working.

I've tried in Chrome and IE and neither will login. I've tried deleting the contents of the storage folder multiple times, and emptying cookies on the browser side. I did think it may be to do with the time being different on the vm and the local machine but they're synchronised.

Any ideas what could be causing this? Anyone come across this issue before?

edit: I've also now recreated the virtual machine and the problem still exists

second edit: I've now started from scratch using a digitalocean VPS with the same results.

I've stripped out everything in my routes file and all it now has is the following

<?php

Route::get('/sess/set/{value}', function($value) {
    Session::set('testv', $value);
Return 'Set ' . $value . ' - ' . Session::get('testv');
});

Route::get('/sess/get', function() {
    Return 'Get ' . Session::get('testv');
});

I visit the first page and it shows whatever value I put into the session. Hit the second page and you only get the 'Get ' part without any session value.

session_attributes always has a _token field, I've tried changing the name of the session and the domain and still can't get sessions to work.

edit3: for anyone who comes across this, I never identified the issue. I started a completely new project, copied my controllers and views across, recreated my routes file and everything is now working - although I'm half expecting to be updating this post tomorrow to say it's broken again!


回答1:


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/




回答2:


I had this exact problem and with almost identical environments as well. As you experienced as well what ended up working for me was starting a totally fresh Laravel 4.1 project and then copying everything over. However I did also find that by changing one config variable I was able to fix the problem:

In /app/config/session.php change the lifetime config item to something higher than 0. This fixed the issue for me.




回答3:


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



回答4:


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.




回答5:


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)




回答6:


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.




回答7:


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.




回答8:


I've experienced issues related to this. The session file wasn't created every time but sometimes I just can't get the session variable displayed. After hours of experiments I found that the problem was related to Debugbar.

If you're using Debugbar and having session issues, disable it and try again to confirm. There's an issue reported here.



来源:https://stackoverflow.com/questions/21076945/laravel-sessions-regenerating-on-every-load

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