views:

115

answers:

3

Is it safe to store a password in a sessions variable?

For example, usage would be in a form which is submitted to itself.

For example a change classifieds page, where users first enter a password, and then if pass=ok, show the form to change the classified. All on same php-page.

But Whenever a picture is uploaded in the "change" part of the php page, the form must submit to itself again.

Should I here use the stores Session password to verify that the user is actually the user, and that it is secure?

In other words, is it safe to store something like this:

 if($pass==$row['password']){ // If password was correct
    $_SESSION['pass_ok']='1';
 }

Thanks

A: 

I would advise against it. If someone logs in and copies the session ID down they can theoretically log in to any page. I would instead advise you check the password is okay on every page refresh as this will be more secure.

Additionally, always store passwords hashed in a database, or better yet, hashed with salts.

Thomas O
How about storing it encrypted in a php session? And the decrypt it everytime, so that the user don't have to enter it everytime they upload a picture?
Camran
Well, you wouldn't encrypt it; you'd hash it. So you'd pass it through something like sha1, then when the user enters their password verify the passwords match by hashing their input and comparing the two. Store the input they used as a hash in a session or cookie, and verify it against the password in the database.
Thomas O
Both your answers are bad practices. You should store the PHP session ID, IP, browser ident, and verification that they are authenticated. Then use all that information to approve that the user is valid.
Petah
Storing a hash in a cookie is a bad practice. I agree. But I was suggesting something that would be better. An even better system would be a sessions system, but not using php sessions, and with a database instead.
Thomas O
-1 **ALL** web applications must store an logged in state in a session variable. The alternative would be to store the password as your cookie, which is far far worse because now your password is a session id that never expires.
Rook
Ouch. -2... I think people are misunderstanding me. Sessions and cookies do expire. Said non-PHP sessions would be locked to one IP.
Thomas O
+1  A: 

Camran, what you are trying to do is a standard way to maintain php sessions. You are actually not storing the password in the session rather just storing the information that this particuar user has already logged in. $_SESSION['pass_ok']='1';

On every page you just have to do a session_start() and check of this session is already set to 1, if yes they assume him to be logged and proceeed, else redirect to login page.

If someone gets hold of the session id then they definitely can access the user session. You can do a few things to make it more secure.

  • Use SSl (https), it will make hard to sniff the data and get your session id
  • maintain the client ip in the session when user logs in, for every request after logging in, check if the requests are coming from same ip
  • Set a short session timeout, so that if left idle for a while the session times out automatically.
Nands
+1 The user can't modify his own session variables, so often we just put a username in `$_SESSION['user']` or similar if a user is logged in. If that value is missing, the user is anonymous.
Adam Backstrom
+1  A: 

Use a pre-built authentication system. That your best bet at being secure because they would have (or should have) thought of everything (security issue) already.

Petah