Cookies vs Sessions

Cookies vs Sessions

·

4 min read

The concept of Cookies and Session is very fundamental and every developer should know this.


A cookie is a small file with a 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 cookie name and value. A cookie can only be read from the domain that it has been issued from.

A cookie created by the 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.

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 can 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.


When to use cookies?

Cookies allow us to track the state of the application using small files stored on the user's computer. The path where the cookies are stored depends on the browser.

When to user Sessions?

To store important information such as the user id more securely on the server where malicious users cannot tamper with them.

Sessions are used to pass values from one page to another. It is also used when you want an alternative to cookies on the browser that does not support cookies.


  • Cookies are client-side that contain user information
  • Cookie ends depending on the lifetime you set for it
  • The official maximum cookie size is 4kb
  • A cookie is not dependent on session

Session

  • Sessions are server-side files that contain user information
  • A session ends when a user closes his browser
  • 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 session is dependent on Cookie

😎Thanks For Reading | Happy Coding😫 %%[bmc]

Did you find this article valuable?

Support Rahul by becoming a sponsor. Any amount is appreciated!