Sessions or Cookies?
Tags: codenewbie, womenintech, productivity, webdev
Published: 2021-06-11 09:24:16

Are you aware of the term __‘Session’__? Let me guess! A session according to you might be, _a period devoted to a particular activity_, right? You are right! BUT…

Here we are going to discuss about Sessions in Computer Science. So, in Computer Science, the term ‘__Session__’ can be used in different contexts such as session layer, web sessions, connections etc. 
But here we will look into the sessions used in __web applications.__

Primarily we know that, _cookies store the user (our) information in form of texts in our computer_. Sessions do the same BUT…

Sessions store the user (our) information temporarily at the web server. Didn’t sound convincing?

Basically, _a **Web Session** is a sequence of adjacent events or actions done by a visitor on a particular website within a given time frame._

We can think of sessions in an easier way, i.e., Sessions can be considered as the time taken by the visitor to browse a particular website. _It represents the time interval between the user’s first arrival on the web page and the time when the user closes the site._
__Fact__: _A developer who codes for the website, initializes and sets the time limit of the session for almost around 5 mins or shorter. So, a single session doesn’t last beyond a certain duration._

After all of these, you might be wondering about the Whys, aren’t you?

#Why do we need Web Sessions?#

Because, sessions store user data in the form of session ID, which is unique for every user. Now, a curiosity might arise about where are these sessions being stored?
 
Like Cookies, are they also stored in user’s hard-disk (computer)?

No. These sessions are stored in the __server-side.__ But, why Servers?

Because, on the internet, _the server doesn’t know you, because the HTTP request sent to the server is stateless, i.e., it does not maintain the state (also the time interval you can say) of the user._

Let us understand this with a small example.

Let’s start from the moment you open your web page and search for an image. The moment you search for it, you sent a HTTP request to the server for the image you searched via the URL which is present at the top of the web page. Along with the http request, you send or tell your session ID to the server – “Hello there, it’s me [Your Name]!”, so that the server recognizes you.

After the server receives both the request and session ID, it checks your session ID and after verifying, the server sends you back the Session ID along with your desired output.
 
One interesting thing might amaze you now. Curious?

#Did you know?#

__Sessions allow the server to manage session state.__
 
Manage state? What does that even mean?

Since HTTP is a stateless protocol, it does not keep track about the past or future commands. So, sessions here, play an important role for the servers to maintain the session state.
 
Let us discuss a small instance. 

Suppose you suspect that, somebody has logged in as you from another device, you got some notification via your mail and within that time all the other sessions got invalidated, i.e., __“log me out everywhere”__ functionality worked. This happened because, Sessions got your back!

Another instance would be when you want to associate, not only your shopping browser but also the shopping cart with another device, you might log in to the other device, you will find all your account information and shopping cart information intact.

All of these happened because, Sessions got it again!

By now you might be thinking that if cookies could do the desired work, why need sessions? 

If both Sessions and Cookies are THIS similar then, 
__Where is the difference?__

Okay so, let’s have a sneak peek into the differences now!

1.__Storage:__ Your website can store the cookies on your computer’s hard disk. 
Whereas, Sessions are being stored in the server-side.

2.__Time-duration:__ The cookie maintains your information until it is being deleted by you from your computer. 
Whereas, Sessions maintains your information until you are on the particular web page. Once you close it, the session is being destroyed.

3.__Disable or Enable:__ You (the user) can disable or enable (by clicking on ‘Accept Cookies’ on some websites) the cookies whenever you want. This can be done on the browser settings.
Whereas, you (the user) cannot disable or enable sessions whenever you want.

A Fact that might amaze you is…

_Sessions are also known as **Non-Persistent Cookies.** Because they are saved on your browser while the browser is running._

After unfolding all the facts, we can say that both the sessions and cookies give the best of them. Despite of all the easy aspects of Sessions, we have to note that more the sessions save, more memory space it occupies. 

Websites with more online users, create a pressure on the server memory and as a result they rely on cookies.

As a whole, we need both sessions and cookies to make our browsing experience smoother.

_Have a safe browsing!_