Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel sessions regenerating on every load

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!

like image 723
Alex C Avatar asked Jan 12 '14 16:01

Alex C


People also ask

What is session regenerate laravel?

Laravel utilize basically the original session file store but acts a little bit different. Beside changing the directory they are saved, when you call the regenerate function it creates another session file and deletes the old one. You can see it the implementation Illuminate\Session\Store. php .

How long does session last laravel?

How long does laravel session last? You need to understand what happened: You've set the lifetime of the sessions to 120 minutes, which means after 120 minutes the session is flushed. The remember_me feature is using cookies.

How to handle session in laravel?

To access the session data, we need an instance of session which can be accessed via HTTP request. After getting the instance, we can use the get() method, which will take one argument, “key”, to get the session data.


1 Answers

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/

like image 89
bearc0025 Avatar answered Sep 21 '22 11:09

bearc0025