When a user selects the remember me function, I save his username and id in a cookie. Then, when the user returns to the site, I check the username and id against a database to ensure that the user is legitimate. I next log the user in by storing the cookie data in the session variable. Is this the proper way to remember and log in a user?
Share
Cookies are not a very secure way of storing data. The cookies can be modified by a user and could lead to someone “hacking” into your site. What I would suggest is store a string in the cookie that is some hash of something. Also store the hashed string from the cookie in your database. This way when a user returns to the site, you check to see if the cookie is filled, match it to the hashed value in the database and then find who owns that hashed value. If all is valid, log them in.
Database setup
pseudocode
In the valid until field you would set it to say 2 weeks from login. once the valid until has passed, do not let that key work and make sure the cookie is expired for the user.
query to check login
SELECT * FROM rememberMe WHERE key=”//put md5 here” AND validUntil > time()