skip to Main Content

The problem is that I need to do an action the moment a session expires, regardless of whether the browser is open or not.

I have a project in Laravel and it is registered when the user is online or offline, when he logs into the system, it registers in the database 1 for the user who is online, and when he leaves, it registers 0. I want it to register 0 when the session expired without the user needing to refresh the screen. Even if the user closes the browser without logging out, the system will register that the user is offline when the session expires.

My English is not good and this is my first question here on the platform.
Thank you anyway!

2

Answers


  1. Chosen as BEST ANSWER

    liquid207 thanks for replying! I am using file as session driver

    I kept doing some testing and researching further and found this link which discusses this very issue:

    Laravel session timeout, extra logout code

    In this case, a middleware is used, but if the user closes the browser without exiting, even with the browser closed, will the middleware work?


  2. Method SessionHandlerInterface::gc() will be invoke when session expires. You can override this method according to the following steps:

    I suppose that you use database as session driver.

    1. Create a class extend DatabaseSessionHandler, override gc
    2. Create a class extend IlluminateSessionSessionManager, override createDatabaseDriver to return class defined in step 1
    3. Register class defined in step 2, like
         $this->app->singleton('session', function (Application $app) {
              return new CustomSessionManager($app);
          });
      
      
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search