Difference Between Cookie and Session

Key Difference between Session and Cookie

  • Sessions are server-side files that contain user information, whereas Cookies are client-side files that contain user information.
  • Session is dependent on Cookie, but Cookie is not dependent on a session.
  • Session ends when a user closes his/her browser, while a Cookie expires depending on the lifetime you set for it.
  • During the session, you can store as much data as you like, whereas the maximum cookie size is 4KB.
  • Session, you can use Session_destroy(), which is used to destroy all registered data or to unset some, while in Cookie does not have a function named unsetcookie()
Difference Between Session and Cookie
Session vs Cookie

What is a Session?

A session is a global variable stored on the server. Each session is assigned a unique id which is used to retrieve stored values. Whenever a session is created, a cookie containing the unique session id is stored on the user’s computer and returned with every request to the server. If the client browser does not support cookies, the unique session id is displayed in the URL. Sessions have the capacity to store relatively large data compared to cookies.

The session values are automatically deleted when the browser is closed. If you want to store the values permanently, then you should store them in the database.

Just like the $_COOKIE array variable, session variables are stored in the $_SESSION array variable. Just like cookies, the session must be started before any HTML tags.

What is Cookie?

A cookie is a small file with the maximum size of 4KB that the web server stores on the client computer. Once a cookie has been set, all page requests that follow return the cookie name and value. A cookie can only be read from the domain that it has been issued from. For example, a cookie set using the domain www.guru99.com cannot be read from the domain career.guru99.com. Most of the websites on the internet display elements from other domains such as advertising. The domains serving these elements can also set their own cookies. These are known as third party cookies. A cookie created by a user can only be visible to them. Other users cannot see its value. Most web browsers have options for disabling cookies, third party cookies or both.

Session vs Cookie – Difference Between Them

Here are important difference between Session and Cookie:

Cookie
Session

Cookies are client-side files that contain user information

Sessions are server-side files which contain user information

Cookie ends depending on the lifetime you set for it

A session ends when a user closes his browser

You don’t need to start cookie as it is stored in your local machine

In PHP, before using $_SESSION, you have to write session_start(); Likewise for other languages

The official maximum cookie size is 4KB

Within-session you can store as much data as you like. The only limits you can reach is the maximum memory a script can consume at one time, which is 128MB by default

A cookie is not dependent on Session

A session is dependent on Cookie

There is no function named unsetcookie()

Session_destroy(); is used to destroy all registered data or to unset some

Why and when to use Sessions?

To store important information such as the user id more securely on the server where malicious users cannot temper with them. Sessions are used to pass values from one page to another.

It is also used when you want the alternative to cookies on browsers that do not support cookies, to store global variables in an efficient and more secure way compared to passing them in the URL, developing an application such as a shopping cart that has to temporary store information with a capacity larger than 4KB.

Why and when to use Cookies?

Http is a stateless protocol; cookies allow us to track the state of the application using small files stored on the user’s computer. The path were the cookies are stored depends on the browser. Internet Explorer usually stores them in Temporal Internet Files folder. Personalizing the user experience – this is achieved by allowing users to select their preferences. The page requested that follow are personalized based on the set preferences in the cookies. Tracking the pages visited by a user.