Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best method to protect login cookie data in PHP?

I'm creating a login system in PHP, and I want to know how to best protect the user information string in my cookie. I was thinking of encrypting the string with a key somehow? Is this the best way? I'm kinda new to this.

Thanks in advance.

like image 354
mattsven Avatar asked Jun 21 '10 14:06

mattsven


3 Answers

Don't store sensitive information in cookies. Store a session ID hash to connect the logged in user with their account.

like image 108
Aaron Harun Avatar answered Nov 15 '22 04:11

Aaron Harun


Aaron Harun has the right answer for you. There's basically no need to encrypt such data as long as you store it in a session, because that data never reaches the client/browser/user, as it is all server-side. When you create a session on PHP, it handles the cookie stuff for you, so you don't have to worry about that. In most cases, there is no need to deal with cookies. In security, dealing with cookies is detrimental.

I've seen some sloppy sites that actually store the username in a hidden field on a form, which allows anybody to simply edit their local copy of that form and take actions as whichever user they like. This seems like an obvious problem, but cookies are no better.

If you truly think it's a good idea to design a homebrew authentication system, you need to design the database first. Don't store plaintext passwords, store a hash instead (like md5, sha-1, etc) and at that point there's no harm in generating a salt for each password (a random string that you append to the user's password before hashing it, and store that salt with the password hash because you'll need it later--this prevents dictionary hash attacks, ie rainbow tables).

like image 32
andyortlieb Avatar answered Nov 15 '22 03:11

andyortlieb


You should never store secure information in a cookie. Cookies are saved in textformat on the user computer, and there are many reason why you should never stock sensitive informations in them :

  1. Cookies are basically text files, which can be opened by anyone on the computer, with any text editor.
  2. The cookies are stored on the user computer, this mean he have no time limit, no connection limit, no processing limit, so he can try to brute force any data as much as he want without being worried of getting ip banned/kicked...

You should only stock things like a username to remember or a session id.

like image 29
Dominique Avatar answered Nov 15 '22 02:11

Dominique