Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 - stay logged in forever

I wanted to put in the login page a checkbox "stay logged in".

That should never end the session of the user unless the user logs out.

I know that in app/config/session.php I have these variables:

'lifetime' => 60,

'expire_on_close' => false,

But I wanted to maintain alive the session forever, and not be removed if the user close his navigator.

Any ideas on how should I proceed? Thanks

like image 720
dani24 Avatar asked Feb 02 '17 19:02

dani24


People also ask

How do I keep a session alive in laravel?

If you want to increase your session life time then you can easily do it from configuration file in laravel. laravel provide session. php there is a 'lifetime' key option for setting time in minutes. in session configuration file there is a also several option for set driver, timeout, expire_on_close and encrypt etc.

How do I increase my session lifetime in laravel?

If you want to increase your session life time then we need to change in . env file and it is very easy to change it from configuration file in laravel. laravel provides you session. php file there is we can see 'lifetime' key option for setting time in minutes.

How does laravel handle session timeout?

By default it is set to 1440. Just comment or delete it. From this time on you session may work properly using your session. php config values.

How do you create remember me in laravel?

Your users table must include the string remember_token column, which will be used to store the "remember me" token. if (Auth::attempt(['email' => $email, 'password' => $password], $remember)) { // The user is being remembered... }


1 Answers

Laravel has a built-in feature to remember users, I'd strongly recommend using it.

If you would like to provide "remember me" functionality in your application, you may pass a boolean value as the second argument to the attempt method, which will keep the user authenticated indefinitely, or until they manually logout. Of course, your users table must include the string remember_token column, which will be used to store the "remember me" token.

if (Auth::attempt(['email' => $email, 'password' => $password], $remember)) {
    // The user is being remembered...
}

if you want to solve the problem manually, there is workaround for this: you have to set the expires date of your cookie to a date in the wide future, somewhat like 2050. this is technically not forever, but should usually be long enough.

like image 136
passioncoder Avatar answered Nov 13 '22 01:11

passioncoder