Loading…

Explain the difference between session and cookies in PHP?

Session and Cookies both functions are used to store information. But the main difference between a session and a cookie is that in case of session data is stored on the server but cookies stored data on the browsers.

Sessions are more secure than cookies because session stored information on servers. We can also turn off Cookies from the browser.

Example

                                        


session_start();
//session variable
$_SESSION['user'] = 'BestInterviewQuestion.com';
//destroyed the entire sessions
session_destroy();
//Destroyed the session variable "user".
unset($_SESSION['user']);

 

Example of Cookies

setcookie(name, value, expire, path,domain, secure, httponly);
$cookie_uame = "user";
$cookie_uvalue= "Umesh Singh";
//set cookies for 1 hour time
setcookie($cookie_uname, $cookie_uvalue, 3600, "/");
//expire cookies
setcookie($cookie_uname,"",-3600);

Suggest An Answer

No suggestions avaliable!

Latest post from PHP interview questions

Ask Question