Cookies VS Sessions

Talk about anything in here.
Post Reply
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Cookies VS Sessions

Post by Temor »

Right, so I've always wondered what the difference is between cookies and sessions. And why/when should I use cookies over sessions and vice versa...

Can anybody give me a crash course in cookies? :P I'm too darn lazy to Google it!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Cookies VS Sessions

Post by jacek »

cookies are similar to session in terms of functionality, but what actually goes on is not the same at all.

When you set a cookie a header is sent to the users browser that contains the data that you set, the browser remembers which site stored which data and sends it back to the website when you visit it as a header, php processes this header and makes the $_COOKIE variable available. The key thing is that the data is stored client side, not on the server.

When you set a session variable, the server sends a unique id (the session id) to the browser as a cookie, as described above the browser will send this back. PHP intercepts this id and uses it to check the session storage (usually files) to see if there is any session data already, if there is it creates the $_SESSION variable form the data. The key thing here is that the data is stored on the server and not client side at all, meaning there is no way for the user to manually change the data in the session like they can with cookies.
Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Cookies VS Sessions

Post by Temor »

jacek wrote:cookies are similar to session in terms of functionality, but what actually goes on is not the same at all.

When you set a cookie a header is sent to the users browser that contains the data that you set, the browser remembers which site stored which data and sends it back to the website when you visit it as a header, php processes this header and makes the $_COOKIE variable available. The key thing is that the data is stored client side, not on the server.

When you set a session variable, the server sends a unique id (the session id) to the browser as a cookie, as described above the browser will send this back. PHP intercepts this id and uses it to check the session storage (usually files) to see if there is any session data already, if there is it creates the $_SESSION variable form the data. The key thing here is that the data is stored on the server and not client side at all, meaning there is no way for the user to manually change the data in the session like they can with cookies.
Alright, makes much more sense now.

But why do people use cookies for say, a forum, instead of sessions? Is it just to keep the user logged in or to use a " remember me " function?
Dominion
Posts: 32
Joined: Thu May 05, 2011 11:32 pm

Re: Cookies VS Sessions

Post by Dominion »

Cookies for a login can be fine, but you must encrypt any data stored in them. To be honest sessions are better for it. Yes people use cookies as both "remember me" functions, and for login's.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Cookies VS Sessions

Post by jacek »

Cookies last longer too, you could have a cookie that is set to expire after a year, if you did the same with the session expire time you would fill your server HDD with session data ;)
Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Cookies VS Sessions

Post by Temor »

So, basically the only thing that cookies do better than sessions is that they are stored client-side instead of server-side, so it saves me some space on my HDD?

In that case, screw cookies! My server has more than enough space left to let me work with sessions instead :)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Cookies VS Sessions

Post by jacek »

Temor wrote:So, basically the only thing that cookies do better than sessions is that they are stored client-side instead of server-side, so it saves me some space on my HDD?
Thats not the main point really, but yes ;)
Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Cookies VS Sessions

Post by Temor »

jacek wrote:
Temor wrote:So, basically the only thing that cookies do better than sessions is that they are stored client-side instead of server-side, so it saves me some space on my HDD?
Thats not the main point really, but yes ;)
Hehe, okay :)

Also, do you know what the default expire time is for sessions?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Cookies VS Sessions

Post by jacek »

Temor wrote:Also, do you know what the default expire time is for sessions?
24 minutes with no page loads I think.
Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Cookies VS Sessions

Post by Temor »

jacek wrote:
Temor wrote:Also, do you know what the default expire time is for sessions?
24 minutes with no page loads I think.
Ok, thank you :)
Post Reply