Complete Guide To Difference Between Session and Cookies

The Complete Guide To Difference Between Session and Cookies

COOKIES:

A tiny piece of information stored at the user’s browser is known as a PHP COOKIE. Cookies store user’s information like username, email id, etc. 

It is stored on the user’s computer or laptop or mobile as a text file. A cookie is created on the server-side

It is used to recognize the user. A cookie can store only a limited amount of data. The maximum size of a cookie is 4KB. 

For example, whenever we visit a website, cookies get stored on our computer. After some time when we send a request to the browser for the same website, then the cookies are also sent with the request. 

In this way, the website recognizes the user with the help of the cookies.

Cookies have security issues as they are stored on the computer of the user, so the content of the cookie can be easily modified or accessed by the attackers.

HOW TO CREATE A COOKIE:

A cookie can be created with the help of the setcookie() function. The setcookie() function should be used before thetag.

SYNTAX: setcookie( name, value, expire, path, domain, secure) The details of the parameters are mentioned below:

Name: It states the specific name of the cookie

Value: It states the value of the cookie.

Expire: It states the time at which the cookie will expire. After this time cookie cannot be accessed. If no parameter is specified or the parameter is set to 0, then the cookie expires automatically when the web browser is closed. The default value is 0.

Path: It states the path for which cookie will be available. If the parameter is set to “/”, then the cookie will be available within the entire domain.

Domain: It states the domain name of the cookie.

Secure: It states that a cookie should be transferred only when a secure HTTPS connection is available.

SESSION:

When the data gets stored on the server and not on the computer of the user, it is known as a Session in PHP

It stores data in the variable form to use it across multiple web-pages. It can store an unlimited amount of data. 

When we are browsing through the internet, many times a message is displayed on our screen as “session has expired“. 

This happens due to the period of inactivity on a web-page and the session gets finished. The session also gets finished when we close the window of the browser.

HOW TO START A SESSION IN PHP:

To start a PHP Session, we need to use the session_start() function. If there is an existing session, then the existing one starts, or else a new session starts. 

A unique session id is also created for the user. After a session is started, we can create session variables to store information. 

We can set session variables with the help of PHP global variable $_SESSION[ ].

Difference Between Session and Cookies :

Difference Between Session and Cookies

Some of the difference between session and cookies are as follows:

  • Cookies are stored on the user’s computer. 
  • Sessions are stored on the server.
  • A cookie can store a limited amount of data, a maximum of 4KB. 
  • The session can store an unlimited amount of data.
  • The cookie does not depend on the Session.
  • The session depends on the cookie.
  •  The cookie expires according to the time of expiry set for it
  • The session expires after the user closes the web browser.
  •  Cookie has many security issues as it can be accessed by anyone easily.
  • The session is secure as it cannot be accessed by anyone easily.
  • There is no function available to disable a Cookie.
  • A Session can be disabled by using the session_destroy() function.
  • The setcookie() function should always be used prior to tag.
  • The session_start() function should always appear prior to tag. 

CONCLUSION:

Here we discussed everything about the Session and Cookies and, The Difference Between Session and Cookies. Hope you liked this article. We have tried to explain the concept in a simple language. 

I hope this article is useful in understanding the concept and also the differences between them.

We will be back with another great article that would prove an add to your technical knowledge.

Thanks for reading….

Leave a Comment