Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel session key changing the whole time?

I am using "file storage" for my session. When I run this:

Session::set('awesomekey', 'myVal123');

And refresh the page, I can see new files being created in /storage/session each time. I assumed it would update the same file each time. This basically means sessions don't work at all. In other words, if it keeps recreating a session file, this never works:

Session::get('awesomekey');

Or at least, it returns a blank. What am I missing that could possibly be causing a new session key to be created each time a page is loaded?

UPDATE

On further investigation, it seems the cookie is regenerated on each page load. What could be causing that?

I am not even looking at logging in yet, so this information is useless to me --> http://willworkforbanjos.com/2014/02/laravel-sessions-not-working-in-4-1/

My problem is happening when I simply put the above set and get code in the master.blade.php file. It should set it, store the info, and on the next reload get the right information from session. But it can't because on reload it changed the cookie to some other code.

Anyone know why this is happening?

UPDATE 2

  1. Adding: 'lifetime' => 120 to session.php did not work. (@Sheikh Heera)
  2. Placing the code in the controller only, does not work. (@Phill Sparks)
  3. I tried chrome and firefox, same result (@The Shift Exchange)

Just to be clear on what I'm trying to do. I add the following code in HomeController.php:

public function index()
{
    Session::put('awesomekey', 'myVal123');
    return View::make('home.index');
}

Then I put this in my master.blade.php:

print Session::get('awesomekey');

I do not include any "dies" or random echos in my controller side of the code, except for this. When I open the file the first time, I can see myVal123 being printed out.

I then take out this part in the controller:

Session::put('awesomekey', 'myVal123');

And reload the page. It now prints nothing. I can see in my browser that the cookie has changed. Generating a new cookie will lose the reference to the session, so I'm stuck trying to understand why it's doing that each time, even though it saves the session on the first load.

Any more ideas?

UPDATE 3

I also tried:

  1. Running "php artisan dump-autoload" ... still doesn't work
  2. I went here: http://www.whatismybrowser.com/are-cookies-enabled ... and yes, cookies are enabled.

I'm really running out of ideas here...

UPDATE 4

  1. I went to SessionManager.php and just underneath this:

    $lifetime = $this->app['config']['session.lifetime'];

I printed out the value of life time:

print $lifetime; die();

And this code was never hit on page reload?! However, adding this in my controller:

$d = Config::get('session.lifetime');
print $d;

Does in fact print out my value for lifetime.... :(

like image 926
coderama Avatar asked Mar 01 '14 08:03

coderama


1 Answers

The problem was this line:

'cookie' => 'xxxx.com',

in session.php. Apparently it loses the cookie if you have a "." in the cookie name. I can't believe Laravel doesn't like that. Or maybe it's browser's in general.

like image 186
coderama Avatar answered Sep 21 '22 06:09

coderama