After session destroy or close browser tab or close browser execute logout using Laravel 5.2

15,327

Solution 1

The server does not know if the user has closed the browser window. You need to detect this event via javascript on the client side and notify the server manually.

See this answer: javascript detect browser close tab/close browser

Solution 2

Do this 'expire_on_close' => true in app/config.php if you want users session to expire or destroy only when the entire browser is closed not the tab. If only the tab is closed it wont destroy the session except the entire browser is closed.

Share:
15,327
sandip kakade
Author by

sandip kakade

I working on multiple languages

Updated on July 31, 2022

Comments

  • sandip kakade
    sandip kakade almost 2 years

    In my project I am using session destroy method which very simple way in Laravel 5.2 .

    /*
        |--------------------------------------------------------------------------
        | Session Lifetime
        |--------------------------------------------------------------------------
        |
        | Here you may specify the number of minutes that you wish the session
        | to be allowed to remain idle before it expires. If you want them
        | to immediately expire on the browser closing, set that option.
        |
        */
    
        'lifetime' => 10,
        'expire_on_close' => true,
    

    Now my question is when session destroy automatically or user close browser tab or close browser that time execute logout query. Is it possible execute logout function all cases?

    My logout function

    public function logout() 
    {
        $user = Auth::user()->toArray();
        $user1 = ActiveUsers::where("assinedto_id",'=',$user['_id']);        
        $user1 ->delete();
        Auth::logout();
        return redirect('/login');
    }
    

    I want to when session destroy or close browser tab or close browser that time run logout() function. Please suggest me. Thanks

  • sandip kakade
    sandip kakade over 7 years
    but if session is automatically destroy like after 10min destroy that time i want to call function logout(). So how can i call this function after session destroy
  • Matey
    Matey over 7 years
  • Chirag
    Chirag over 4 years
    Have you tested this? We tried doing this and actually doesnt work.
  • Digvijay
    Digvijay almost 4 years
    @Chirag After changing any config, you have to run php artisan config:clear and php artisan cache:clear
  • Adam Pery
    Adam Pery almost 3 years
    Actually 'expire_on_close' existing in 'config/session.php'