Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johnnemo's avatar

Session key destroyed (L5.1)

Hello,

I try to keep a key in my session for a specific user when he logs in. I store the current login timestamp as follows

if($attempt){
        $request->session()->put('login', Carbon::now()->toDateTimeString());
}       

and pass it to the session. This works just fine. I also use the remember me approach (with the appropriate remember_me token). The problem is when I close the browser the login-token is being destroyed. Is there any way to keep this value..? Or should I go to another session driver (e.g database) ..?

0 likes
7 replies
jrean's avatar

@johnnemo when you close the browser you also loose the "remember me" in session user?

johnnemo's avatar

@jrean thank you for answering. Well no, login_token and _token are still there... I lose the _url, _previous, _flash and the custom login session key I added.

bobbybouwmann's avatar

If you look closely to the authentication they they create a cookie instead of a session. A cookie is saved when the browser is closed, a session is not!

bobbybouwmann's avatar

If you want to save something in the browser even when the browser is closed, you need to use a cookie.

It sounds to me that you are building a throttle system for logins. I recently build this myself as well and my solution was to store a throttle in the database and check there.

johnnemo's avatar

@bobbybouwmann , Well I try to keep some statistics about the users..How long did the watched a page, etc..So I need a way to keep both the login timestamp and the logout one (easy part). In case I use a cookie I assume that I have to delete it in the destroy-logout function right..? Any case, the documentation about cookies is a little poor I think..So, should this work...?

   return response()->view('something.show', compact('var1', 'var2'))->withCookie('name', 'value', $minutes); 

And then retrieve it like

  $request->cookie('name');

And in the destroy-logout function how could I delete it..?

bobbybouwmann's avatar
Level 88

You need to create the cookie and pass that

$cookie = cookie('name', 'value', $minutes)

return response()->view('some.view')->withCookie($cookie);

You can delete the cookie on request as well

$cookie = Cookie::forget('name');

return redirect()->route('home')->withCookie($cookie);
1 like

Please or to participate in this conversation.