My login system currently works as follows:
- Grab the user's username and password from a POST form.
- Check the username and salted + hashed password against the database.
- If authentication is successful, generate a long, random, alphanumeric string.
- Store this string in MySQL, as well as in a
$_SESSION
variable along with the username.- This is opposed to just putting the user's password in
$_SESSION
. I feel like that's leaving the door wide open for spyware to come in and steal the user's credentials.
- This is opposed to just putting the user's password in
- On each page that requires authentication (e.g. non-sensitive account settings, members-only areas, etc.), check the
$_SESSION
username and string against those stored in MySQL. - If they match, go ahead and show the page. Otherwise, show a login form.(?)
- When the user explicitly logs out, remove the random strings from MySQL and
$_SESSION
.
What I'm stuck on is how to handle when the user implicitly logs out. That is, when he/she closes the browser window without hitting any "log out" button on the site. I'm pretty sure I still need to remove the random string from MySQL, so someone can't use a stolen cookie to log in afterwards. But how do I know when the user closes the browser window?
("Remember me" functionality is irrelevant for now.)