What is the difference between a session and cookies in PHP?

Last updated 4 years, 12 months ago | 1268 views 75     5

Tags:- PHP

Session V/S Cookies

A cookie is an array of data stored by the browser on the client location and sent to the server with every request. It stores a limited amount of data about 4kb(i.e.: 4096 bytes). It is less secure than a session because it present on the client's computer.

The session is an array of data stored on the server and associated with a given user (usually via a cookie containing an id code). It stores an unlimited amount of data. It is more secure because of stored on the server.


Example of Cookie

  setcookie(name, value, expire, path, domain, secure, httponly);

  $cookie_uname = "user";

  $cookie_uvalue= "Retesh Kumar";

  //set cookies for 1 hour time
  setcookie($cookie_uname, $cookie_uvalue, 3600, "/");

  //expire cookies



Example of Session


//session variable
$_SESSION['user'] = 'Retesh';
//destroyed the entire sessions
//Destroyed the session variable "user".