My Laravel 5.2.10 Sessions Wont Persist

My Laravel 5.2.10 Sessions wont persist

Laravel's middleware class \Illuminate\Session\Middleware\StartSession is responsible for starting your session. Before L5.2, this ran on every request because it was part of the global middleware stack. Now, it's optional because L5.2 wants to allow for both a web UI and an API within the same application.

If you open up app/Http/Kernel.php, you'll see that the StartSession middleware is part of a middleware group called web. You need to put all your routes inside there for your example to work.

Route::group(['middleware' => ['web']], function () {
Route::get('/set/{value}', function($value) {
var_dump(Session::getId());
Session::set('test', $value);
return view('welcome');
});

Route::get('/get', function() {
return 'Get ' . Session::get('test');
});
});

You can see that the web middleware group is also responsible for other things like providing the $errors variable on all views.

You can read more about it in the docs:

By default, the routes.php file contains a single route as well as a route group that applies the web middleware group to all routes it contains. This middleware group provides session state and CSRF protection to routes.

Any routes not placed within the web middleware group will not have access to sessions and CSRF protection, so make sure any routes that need these features are placed within the group. Typically, you will place most of your routes within this group:

Source: https://laravel.com/docs/5.2/routing

Laravel session data is lost after click

Try wrapping your routes (inside app/Http/routes.php) in a Route::group() with the web middleware:

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

An easy way to test this:

Route::group(['middleware' => 'web'], function () {
Route::get('', function () {
Session::set('test', 'testing');
});

Route::get('other', function () {
dd(Session::get('test'));
});
});

If you remove the web middleware, you'll receive null since the web middleware is responsible for starting the session.

Ensure you have the web middleware group inside your app/Http/Kernel.php:

protected $middlewareGroups = [
'web' => [
Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
Middleware\VerifyCsrfToken::class,
],
];

Laravel 5 Session not available for the second page

After creating a fresh laravel installation, its working.

Kill duplicate Sessions on Server in Laravel 5.2

As far as I see there is no difficulty with that.

Sample data in this table looks like below:

Session table data

So you have here user_id, browser and last activity timestamp. So if you want you can now add to cron for example running the following query:

select user_id, count(*) AS `total` FROM sessions GROUP by user_id HAVING count(*) > 1

This will give you users with multiple sessions and you'll be able to decide what to do with them. You can for example remove all sessions for those users or leave only the last one and remove all the others. It's up to you of course.

In the moment when you remove the record from database user will need to login again so for example above if I removed my session for Firefox, I need to login again in Firefox to be logged on my account.

EDIT

Be aware that by default there is no sessions table (because many users won't use database driver for sessions). To create this table you need to run:

 php artisan session:table

The above command will create sessions migration

and then you need to run

 php artisan migrate

to apply this migration into database

Session expire time in Laravel

Session is renewed every time the session_start(); is called. This will happen when you for example refresh the page, write data to the session etc. Its not 1800 seconds from the time a session is created, and then it closes after 30 minutes. Its 30 minutes from the last time the session was accessed

Laravel 5.2 Event Testing: expectsEvent not seeing the event fired although it is being fired

Same thing happened to me, $this->expectsEvent() was not detecting that the event was being fired or prevent it from propagating to the event listeners..

Previously, I was firing my events using Event::fire(new Event()). I tried changing it to event(new Event()) and the test suddenly works correctly now, with it detecting that the event has been fired and silencing the event.



Related Topics



Leave a reply



Submit