After session destroy or close browser tab or clos

2020-07-27 05:37发布

问题:

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

回答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



回答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.