The best way to implement a "Remember Me" function for login in php
I'm currently working on a remember me functionality for my login page and i know the common way is to use
setcookie()
then $_COOKIE['<some field here>]
and assign a value and check that to login, but is there a better way or a safer way?33 Replies
I'm curious if I should store the cookie in the database and compare it to the user cookie in the browser local storage (probably not needed just dumb thinking atm) or try something different
remember me just sets the expiration on the cookie from "end of browser session" to some other, higher value
sometimes it also sets the username in a cookie and fills that in when the login page is displayed, either using
$_COOKIE
on the server, or using javascript on the client
probably using session_set_cookie_params
so things like this
maybe throw in some encryption on the cookie?
well, that would be a massive security issue
I could just guess a userid and log in
so no, not that
gimme a sec
like i do this for csrf
I thought I had an example handy, but I don't.
maybe create a token for the database to compare to the
$_COOKIE
?you're overthinking this
just make sure the session lasts longer when a user check remember me
that's a one month cookie right?
yeah
right now this is what I got
i start the session very early in the login page
the alternative would be to set a timestamp in the session and discard the session when it's expired
you'd have to check in some bit of global code though
might go with your first suggestion to not overthink it
my goal is once the admin stuff is added and everything is tested ill put it on #showcase for suggestions on ways to make the php code better and security if needed so i can learn new skills
where would you put the cooke param code in my above code?
gonna give it a shot and see what happens!
hm, I see that would be a problem for the CSRF stuff though, I missed that
$_SESSION
referenceyeah wasn't sure about the password verify being moved where it is
I never really bothered with CSRF for login forms, but it's also complex enough I probably wouldn't roll my own
yeah it does have its uses though
like prg without double form submission and preventing bad actors to an extent
double form submission on a login form shouldn't matter
really? Just registering?
I mean, I guess someone might be able to replay the request if they got ahold of it? But then wouldn't they already have the response anyway?
yeah, i noticed you moved the token generation into the submit
is it only recommend to do that during submit?
I wasn't paying that much attention tbh
oh ok
like I said, I don't have experience rolling my own CSRF
for a future project what's a good way to handle csrf?
dunno, none of the business software I wrote had CSRF 🤣
wild 😄
I think you might be able to use a second named session, one for login and one for CSRF stuff?
anyway, I gotta go do some other stuff
ty for your help 🙂
will update if any cookie issues persist
ran into an issue where my database isn't updating the change for the cookie and cookieexpire fields
these are the new functions in my User model to help process the change
them in the controller
i think the query has to be wrong somehow
All I know is the database isn't updating so the new model commands are wrong or I'm not doing it correctly in the login code