Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

omrakhurs's avatar

How to redirect to login page in Laravel 5.2 on session timeout?

I have set session timeout to 30 minutes in config/session but it is not working. I want to know how I would redirect a user to the login page once the session has expired. At the moment, the session expires after a long time (It's more than 30 minutes, so I don't know where it's getting the value from). So in essence, if someone knows how I should set the timeout to 30 minutes, and then redirect the currently logged in user to the login page with a message that tells them to login again. I have tried using the solution given here: http://laravel-tricks.com/tricks/session-timeout-for-logged-in-user That doesn't work for me.

0 likes
8 replies
Szyfr's avatar

try this, working on my last project.

on your app/Exception/Handler.php

    public function render($request, Exception $e)
    {
        if ($e instanceof ModelNotFoundException) {
            $e = new NotFoundHttpException($e->getMessage(), $e);
        }

        if ($e instanceof \Illuminate\Session\TokenMismatchException) {    

          // flash your message

            \Session::flash('flash_message_important', 'Sorry, your session seems to have expired. Please try again.'); 

            return redirect('login');
        }

        return parent::render($request, $e);
    }
1 like
omrakhurs's avatar

Does this work with the time set in config/session?

omrakhurs's avatar

not working. Here is my config/session :

return [

/*
|--------------------------------------------------------------------------
| Default Session Driver
|--------------------------------------------------------------------------

| Supported: "file", "cookie", "database", "apc",
|            "memcached", "redis", "array"
|
*/

'driver' => env('SESSION_DRIVER', 'file'),

/*
|--------------------------------------------------------------------------
| Session Lifetime
|--------------------------------------------------------------------------

*/

'lifetime' => 1,

'expire_on_close' => true,

/*
|--------------------------------------------------------------------------
| Session Encryption
|--------------------------------------------------------------------------

*/

'encrypt' => false,

/*
|--------------------------------------------------------------------------
| Session File Location
|--------------------------------------------------------------------------

*/

'files' => storage_path('framework/sessions'),

/*
|--------------------------------------------------------------------------
| Session Database Connection
|--------------------------------------------------------------------------

*/

'connection' => null,

/*
|--------------------------------------------------------------------------
| Session Database Table
|--------------------------------------------------------------------------

*/

'table' => 'sessions',

/*
|--------------------------------------------------------------------------
| Session Sweeping Lottery
|--------------------------------------------------------------------------

*/

'lottery' => [2, 100],

/*
|--------------------------------------------------------------------------
| Session Cookie Name
|--------------------------------------------------------------------------

*/

'cookie' => 'laravel_session',

/*
|--------------------------------------------------------------------------
| Session Cookie Path
|--------------------------------------------------------------------------

*/

'path' => '/',

/*
|--------------------------------------------------------------------------
| Session Cookie Domain
|--------------------------------------------------------------------------

*/

'domain' => null,

/*
|--------------------------------------------------------------------------
| HTTPS Only Cookies
|--------------------------------------------------------------------------

*/

'secure' => false,

/*
|--------------------------------------------------------------------------
| HTTP Access Only
|--------------------------------------------------------------------------
*/

'http_only' => true,

];

zz's avatar

php artisan config:cache

Waldemar's avatar
Waldemar
Best Answer
Level 3

@omrakhurs

1 Change 'driver' => env('SESSION_DRIVER', 'file'), to 'driver' => env('SESSION_DRIVER', 'database') in config/session

2 create database sessions

php artisan session:table

composer dump-autoload

php artisan migrate

then try your code if been error show me your error

babonday's avatar

answer maybe correct but some context be helpful...why do you have to create a db table to get the session timeout to work?

Also i changed the time value session_lifetime in my .env file and it worked... didnt work in the session.php file for me either...

still trying to work out how to redirect user to login page after session time out....

Please or to participate in this conversation.