Skip to content

Instantly share code, notes, and snippets.

@loren138
Created December 18, 2015 20:13
Show Gist options
  • Save loren138/c11dfb1abdf4cd89d5af to your computer and use it in GitHub Desktop.
Save loren138/c11dfb1abdf4cd89d5af to your computer and use it in GitHub Desktop.
Laravel Session Timeout Middleware

Install this middle into app/Http/Middleware.

Then add, \App\Http\Middleware\SessionTimeout::class, to app/Http/Kernel.php just after \App\Http\Middleware\StartSession::class,

<?php
namespace App\Http\Middleware;
use Closure;
use Session;
class SessionTimeout
{
public function __construct()
{
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$timeout = config('session.lifetime', 20) * 60;
if (Session::has('lastActivityTime') && (time() - Session::get('lastActivityTime')) > $timeout) {
Session::clear();
}
Session::put('lastActivityTime', time());
return $next($request);
}
}
@chamra
Copy link

chamra commented Dec 6, 2018

hi! I tried this in laravel 5.6 and every time Session::get('lastActivityTime') returns null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment