Cookie versus Session

Difference between Cookie and Session in PHP?


Cookie Session
1. It used to identify a user. 1. It is used to identify a user session.
2. Contain information about the user. 2. Contain information about, or change settings for a user session. They are available to all pages in one application.
3. It is a small file that the server embeds on the user’s computer. 3. Sessions usually store the user information on the server for later use.
4. Cookies may or may not have UIDs. 4. Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID. The UID is either stored in a cookie or is propagated in the URL.
5. Each time the same computer requests a page with a browser, it sends the cookie too. 5. As the sessions reside on server they just need to identify session UIDs which might be stored in a cookie or a URL.
6. The PHP $_COOKIE variable is used to retrieve a cookie variable value. 6. The PHP $_SESSION variable is used to retrieve a session variable value.
7. The cookies can retain user information in the user’s browser until the user blocks or deletes them at any time. 7. The session information is however temporary and will be deleted after the user has left the website.
8. You can directly store the information onto the cookie file using the setcookie(). 8. In order to store the user information in your PHP session, you must first start up the session using session_start().
9. A cookie can be deleted by ensuring that the expiration date is in the past i.e. a time period that has already passed. 9. Ifyou wish to delete some session data, you can use the unset() or the session_destroy() function. The unset()function is used to free the specified session variable. The session_destroy() is used to completely destroy the session.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s