Larry Lawless
Larry Lawless

Reputation: 623

Laravel 5.2.11, sessions are not working, session cookie not being set

I'm new to Laravel, I have learned about the models, views, blade, controllers and routes and how they work together. So far everything has been working smoothly.

I'm having trouble with sessions though.

When I use the AuthController that comes with Laravel and hit auth/register with a POST request, the data for the user that I register does get inserted into the users table (using mysql) and I do get back a response with the "Location" header redirecting to / like it does out of the box. It redirects like it should. But in the same response there is no "Set-Cookie" header set/sent. The session part of Laravel is not working properly for me. This is the same for a POST to auth/login, it authenticates properly and redirects to the profile page but no session cookie is sent back in the response.

I'm using:

All of the php modules that Laravel requires are installed. I'm running the app with php's built in web server. I run that with sudo. The exact command I run is this:

sudo php -S localhost:8888 -t public/

All routes are being responded to properly.

I have tried both ways of installing Laravel that the installation docs recommend, through the laravel executable and composer create-project. Still no cookies set either way. I have made all the files and directories of the laravel project mod 777. The app key is set in .env if that makes any difference.

The config/session.php file is using the file driver for the session. There are no session files in the storage/framework/sessions directory after setting a session.

When I try setting a session myself with the session function like it states in the docs:

session(['sesskey' => 'somevalue']);

Again no "Set-Cookie" header is sent in the response and no session file is created. There are no error messages reported back either I should add.

When I do set a session key with the session function like above I can get that value back however and echo it back to the browser like so:

echo session('sesskey');

So it does seem to save it at least in php's memory.

When I try setting a cookie using the withCookie method, I do get the proper response with the Set-Cookie header set:

return response()->view('welcome')->withCookie(cookie("test", "val" , 3600));

I tried going down the illuminate rabbit hole to see if I could find a problem but that is a bit over my head atm.

Any help would be much appriciated, thanks!

Upvotes: 4

Views: 4085

Answers (2)

Evgeniy
Evgeniy

Reputation: 123

use middleware for the request \Illuminate\Session\Middleware\StartSession::class

Route::group(['middleware' => [\Illuminate\Session\Middleware\StartSession::class]], function () {

});

Upvotes: 0

Imtiaz Pabel
Imtiaz Pabel

Reputation: 5443

in laravel 5.2 you need to use "web" middleware for your problem,like that

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

Upvotes: 10

Related Questions