Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Jin He
Jin He
8,123 Points

About the usage of cookies and session

Hey guys, I'm feeling confused about the usage of cookies and sessions, what kind of data should cookie store, what kind of data should session store, when should I use cookies to store data, when should I use session to store data ????

1 Answer

Logan R
Logan R
22,989 Points

I personally use session for user data, like user ids and usernames because sessions are more secure. Cookies on the other hand can be easily changed so it's better to use it for things like marking yes, the user has read this page, etc.

I am sure that's super vague, so check out this article: http://php.about.com/od/learnphp/qt/session_cookie.htm

A cookie can keep information in the user's browser until deleted. If a person has a login and password, this can be set as a cookie in their browser so they do not have to re-login to your website every time they visit.

Sessions are not reliant on the user allowing a cookie. They work instead like a token allowing access and passing information while the user has their browser open.