Jump to content

how do securely store username and password in cookies ?


jasonc

Recommended Posts

I have had a load of people who are silly enought to forget their username and or password so wish to add in the option for them to save their details in a cookie, and break my number one rule, never to use them!!!

 

can anyone suggest how i can do this so it is stored safely like most websites do it.

Link to comment
Share on other sites

I agree with Dan; it's best not to store login info inside the cookie. You'll be better off storing their table row ID for example. But keep in mind that cookies are very easy to edit if the user knows how. So it would be easy for them to change the ID to whatever they want.

 

So you'll want to dynamically create some other key to help prevent tampering with the cookie.

Link to comment
Share on other sites

Auto-login cookies are generally created using a hash of four things:

1)  Something they know that you don't know (password)

2)  Something you know that they don't know (auto-generated hash stored in their user table, the microtime of their signup time, etc)

3)  Something unique to their machine (IP)

4)  A fixed salt that no user knows.

 

Hash all those things, and store them in the DB along with their username or UserID.  When they come back to the site, load the data for their user, and compare the generated hash on the server with the hash stored on their machine.  If it matches, log them in.  If it doesn't match, kill the cookie and kick them to the login page.

 

-Dan

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.