My website has a login feature and therefor needs to save the user's information in a cookie so that it they don't have to log in every time they go on the website.
To my understanding, cookies aren't secure unless some extra checks are put in place, so I've come up with a way that I think should make them secure (if not, please tell me how it should be):
- create a table in the database that holds cookie information
- within the table, there are two columns; one that holds the user's username, and another that is some large random number (how would I generate the random number in code?)
- upon logging in to the website (if the cookie already exists), it checks with the database to see if the username and the random number match
- if it's the first time the user is logging in (the cookie doesn't exist), it fills the required tables in the database and sets a cookie
- upon logging in successfully, a new random number is assigned to the database as well as the user's cookie
Does this seem like a viable approach?
Also, if multiple pages require to have the person's username from the cookie, should it verify with the database each time it's required or should it just use $username = $_COOKIE["username"]; without any verification?
Thanks.