
What is the difference between a session and cookies in PHP?
Last updated 5 years, 9 months ago | 1838 views 75 5

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
<?php
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
setcookie($cookie_uname,"",-3600);
?>
Example of Session
<?php
session_start();
//session variable
$_SESSION['user'] = 'Retesh';
//destroyed the entire sessions
session_destroy();
//Destroyed the session variable "user".
unset($_SESSION['user']);
?>