Jump to content

Cookies Vs. Sessions


frijole

Recommended Posts

Cookies are stored on the client, session are stored on the server.

 

I typically use sessions for most things, because if a client has cookies disabled it then can make your site inaccessible. If you don't mind having cookies as a requirement... go for it.

 

I use cookies for convenience type of stuff. Store a username in a cookie so that it can be prefilled for them, etc. Obviously you don't want to store sensitive data in cookies... at the very least you would want to encrypt it first.

Link to comment
https://forums.phpfreaks.com/topic/94312-cookies-vs-sessions/#findComment-483053
Share on other sites

cookies reside on the clients machine, and the data can be seen by anyone using that machine. They are not secure, and should only be used for data that doesn't need to be secure, as they don't even need to be hacked - the data is right there for all to see.

 

sessions reside on the server and are more secure. The data inside of them cannot be seen by the user. They are not 100% secure however, so sensitive information shouldn't be stored in them (for example passwords).

 

 

Link to comment
https://forums.phpfreaks.com/topic/94312-cookies-vs-sessions/#findComment-483056
Share on other sites

sessions reside on the server and are more secure. The data inside of them cannot be seen by the user. They are not 100% secure however, so sensitive information shouldn't be stored in them (for example passwords).

How would you go about keeping a user logged into a site then? If the password is unsafe?

Link to comment
https://forums.phpfreaks.com/topic/94312-cookies-vs-sessions/#findComment-483072
Share on other sites

You shouldn't ever store the user's PW in a cookie, encrypted or not.  In the case of a 'Remember Me' function that automatically logs a user in, it would be better to store the username and a unique, site-generated key in the cookie.  This key is attached to the user's record in the database.

 

When anyone visits the site, you check if the username and this key are present in the cookie.  If they are you check if the key matches that in the DB and if it does, log the user in.  This way if someone else uses the machine you don't reveal too much about the previous user if they look at the cookie.

 

Optionally you can encrypt the username and key combination in the cookie for additional security.

Link to comment
https://forums.phpfreaks.com/topic/94312-cookies-vs-sessions/#findComment-483093
Share on other sites

I'm not sure why I'm even responding to this since you are continuing to double post... but oh well.

 

Why would you even need to store a password? You don't. You store a value indicating if they are logged in or not. If this unique identifier is set, you grant access, if it's not then you redirect them to login and give them a chance to login. You would not need to authenticate them on every page.

Link to comment
https://forums.phpfreaks.com/topic/94312-cookies-vs-sessions/#findComment-483109
Share on other sites

After you have checked their login and password info, store either their username or their user id in a session variable, and check for that each time. There is no need to keep checking the password, its insecure and inefficient.

 

Sessions are stored and removed when you restart your browser, so in a session will not work if the user closes the browser.  It has to be stored in a cookie on the users machine.  Store a user name and a unique key is a cookie, and in the database, and check to see if they are set in the cookie, and they are the same.

Link to comment
https://forums.phpfreaks.com/topic/94312-cookies-vs-sessions/#findComment-483365
Share on other sites

so in my members  table there would be:

 

username - this

userid

password

uniquekey -  and this could be set in a cookie?

 

and then if the "remember me" checkbox is checked it will set the cookie?

 

so what does the beginning of my frontpage.php file look like in order to check for the cookie?

Link to comment
https://forums.phpfreaks.com/topic/94312-cookies-vs-sessions/#findComment-483371
Share on other sites

Sessions are stored and removed when you restart your browser, so in a session will not work if the user closes the browser.  It has to be stored in a cookie on the users machine.  Store a user name and a unique key is a cookie, and in the database, and check to see if they are set in the cookie, and they are the same.

 

You are correct about a session not working after closing the browser. But if you want to have a secure system, then you shouldn't store passwords anywhere other than the database. You should require your users to log in each time.

 

You can store passwords in a cookie if you want, but you are basically lowering the security level of your site. If thats not such a big issue, then you have no worries.

Link to comment
https://forums.phpfreaks.com/topic/94312-cookies-vs-sessions/#findComment-483457
Share on other sites

That still doesn't change the fact that any person using that computer can go in and see what that unique key is. Cookies are entirely exposed.

 

Yes, agreed, but for a remember me, you will have to use cookies.  That is if you want the user to stay logged in even if a browser is closed.  That is what I was getting at.

Link to comment
https://forums.phpfreaks.com/topic/94312-cookies-vs-sessions/#findComment-483514
Share on other sites

Not reliably. You can try logging their IP address, and it may work for a day or two, but most people have dynamic IP addresses these days so it wont work after that. You also run the risk of giving the wrong person access to their account if someone happens to end up with the same IP address coincidentally. Not so likely to happen, but its not impossible by any means.

Link to comment
https://forums.phpfreaks.com/topic/94312-cookies-vs-sessions/#findComment-483600
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.