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

拟墨画扇 提交于 2020-05-28 11:56:11

问题


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.



来源:https://stackoverflow.com/questions/39408639/after-session-destroy-or-close-browser-tab-or-close-browser-execute-logout-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!