Difference between Session and Cookies

    Oct 10, 2019       by Pankaj Kumar
difference-between-session-and-cookies.jpg

What is Session?

It is a way to store useful information on the server. Within this, a temporary directory is created on the server where the session variables and its value are stored. This data remains available to all the pages of the website. The session ends normally after 30 minutes. 

 

Example:

 

// For php langauge

$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";

session_unset(); // remove all session variables

session_destroy();  // destroy the session

 

 

What is Cookies?

Cookies are the text files stored on the client system for future use. The main use of cookies is to track the user while visiting websites. In short, A cookie is a bit of data stored by the browser and sent to the server with every request. For example, user's age, name, identification number, etc.

 

Example:

document.cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";

 

Difference between Session and Cookie?

 

Session

  • Data is stored on the server-side
  • Session is more secured than cookie, because data is stored in encrypted form.
  • Session is independent for every client
  • There is no limit of size or number of sessions to be used in an application. Official max Cookies size is 4KB
  • One of its disadvantages is that it is a burden on the server
  • Lifetime of a session can be defined manually
  • Data is store in object format, so any type of data can be stored.

 

Cookies

  • Data is stored on the client-side.
  • It is less secured, Data stored in cookies can be seen easily on the browser because it is stored on test format at the client-side.
  • It may or may not be individual for every client.
  • Its size is limited to 40 and maximum number of cookies to be used is restricted to 20. Data can be stored as much data as you like within in sessions
  • It can be disabled
  • It is less secured than session because data is stored in text format, so confidential data should not be stored in cookies.
  • It can only store string datatype.

Find other similar Articles here: