Cookie related interview Questions

Ques:- What is cookie?
Ans:- Cookies works on client side and it is stored on the user’s computers in the text file format.

Ques:- What is the meaning of a Persistent Cookie?
Ans:- A persistent cookie is permanently stored in a cookie file on the browser’s computer. By default, cookies are temporary and are erased if we close the browser.

Ques:- How to set cookies in PHP?
Ans:- We can set cookie through setcookie($cookie_name,$cookie_value,$time) function.
Example:-

<?php
setcookie("username", "John", time()+3600);
?>

Ques:- How to retrieve cookies value in PHP?
Ans:- We can retrieve cookie through $_COOKIE[$cookie_name].
Example:-

<?php
echo $_COOKIE['username']; //output:-John
?>

Ques:- How to remove cookie in PHP?
Ans:- We can remove cookie through set the expiration date in the past.
Example:-

<?php
setcookie("username", "", time()-3600);
?>