Saturday , November 25 2017
Home / PHP / Difference between Session and Cookies in php

Difference between Session and Cookies in php

The main difference between a session and a cookie is that session data is stored on the server, whereas cookies store data in the visitor’s browser.

                                Sessions                                 Cookies
Session data is store for the browser run. After closing the browser we lose the session information Cookie data is available in our browser. up to expiration date.
A session is a collection of data stored on the server and associated with a given user A cookie is a bit of data stored by the browser and sent to the server with every request.
session is a group of information on the server that is associated with the cookie information Cookies are used to identify sessions
Sessions are not stored in user’s browser Cookies are stored in user’s browser
Session data are not easy to modify as they are stored at server side. Cookie data are easy to modify as they are stored at client side.
It is stored unlimited amount of data

 

It is stored limit amount of data. It is only allowing 4kb[4096bytes]

 

Example of Sessions: When we log in as a member in any website it creates sessions until we log out of the website. Example of Cookies: If we open any e-commerce site the purchase few items then those items are stored in cookies

  Cookies

  • Cookies are stored in browser as text file format.
  • Cookies stored limit amount of data. It is only allowing 4kb[4096bytes]
  • Cookies not hold the multiple variable
  • We can find the cookies values in easily. So it is less secure.

Destroy Cookies in php :

Setting the cookie time to expire the cookie when  browsers are close at the time.

Example

<?php
setcookie(name, value, expire,
path,domain, secure, httponly);
$cookie_uame = “user”;
$cookie_uvalue= “Rohit Nigam”;
//set cookies for 1 hour time
setcookie($cookie_uname,
$cookie_uvalue, 3600, “/”);
//expire cookies
setcookie($cookie_uname,””,-3600);
?>

Cookies  are used to identify sessions

If there were no cookies then you would be sending a unique ID on every request via GET or POST. Cookies are like static id’s that stay on your computer for some time.

 

Session

  • Sessions value are stored in server side.
  • Session are stored unlimited amount of data.
  • Session are holding the multiple variable.
  • Compare to cookies it is more secure.

Destroy Sessions in php:

  • Using unset() function in session, then the sessions will destroyed.
  • Using session_destory(), we will destroyed the sessions.

Example:-

<?php
session_start();
//session variable
$_SESSION[‘user’] = ‘Rohit’;
//destroyed the entire sessions
session_destroy();
//Destroyed the session
variable “user”.
unset($_SESSION[‘user’]);
?>

  • Session data are not easy to modify as they are stored at server side.
  • The main difference between a session and a cookie is that session data is stored on the server and cookies store data in the visitor’s browser.
  • Sessions are more secure than cookies as it is stored in server.
  • The session_start() function must be the very first thing in your document.  Before any HTML tags.

About Rohit Nigam

Leave a Reply

Your email address will not be published. Required fields are marked *