Difference Between Session and Cookie in PHP
Cookies are stored in browser as a text file format. It is stored limit amount of data.It is only allowing 4kb[4096bytes]. It is not holding the multiple variable in cookies.
Sessions are stored in server side. It is stored unlimited amount of data.It is holding the multiple variable in sessions. we cannot accessing the cookies values in easily.So it is more secure.
Cookies |
Sessions |
Cookies are stored in browser as text file format. |
Sessions are stored in server side. |
It is stored limit amount of data. |
It is stored unlimited amount of data |
It is only allowing 4kb[4096bytes]. |
It is holding the multiple variable in sessions. |
It is not holding the multiple variable in cookies. |
It is holding the multiple variable in sessions. |
we can accessing the cookies values in easily. So it is less secure. |
we cannot accessing the session values in easily.So it is more secure. |
setting the cookie time to expire the cookie. |
using session_destory(), we we will destroyed the sessions. |
The setcookie() function must appear BEFORE the <html> tag. |
The session_start() function must be the very first thing in your document. Before any HTML tags. |
Example of Cookie:
<?php
setcookie(name, value, expire,
path,domain, secure, httponly);
$cookie_uame = "user";
$cookie_uvalue= "Hitesh 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'] = 'Hitesh';
//destroyed the entire sessions
session_destroy();
//Destroyed the session
variable "user".
unset($_SESSION['user']);
?>
|