Laravel5 Inactivity idle session logout

In this post I would like to show how to automatically logout user after some period of inactivity. Session timeout or Session expire depends on the server configuration so in order to achieve our target we are going to write middleware in the Laravel5. Without making any changes to server configurations we will use last activity time to logout user.

Open your config/session.php and specify the number of minutes that you wish the session to be allowed to remain idle before it expires.

automatically logout user after some period of inactivity
Create a file called SessionDataCheckMiddleware.php in App\Http\Middleware location and copy past below code.

Now open \app\Http\Kernel.php file, add our custom “SessionDataCheckMiddleware” middleware

That is it.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

Arjun
I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.
  • Laurent Passebecq

    Very usefull. Thanx

  • melt challenge

    Hi, Its really working fine but it will not redirect to login page
    Help please

  • Chamandeep singh ranot

    Hi, Its very usefull code….can you please suggest how to redirect to login page

    automatically if user inactive during session expire

  • bouffekai

    Perfect ! How can I redirect to login page or display an error message ?

  • Raul Flores

    Not work laravel 5,4 help me!!! please

    • Not working means? Can you provide more info?

      • Raul Flores

        Call to undefined method IlluminateSessionStore::getMetadataBag() laraverl 5.4
        Laravel 5.3 work perfect but i have project in laravel 5.4

  • hz

    laravel 5.4 got error..
    Call to undefined method IlluminateSessionStore::getMetadataBag()