How to create and destroy cookies in PHP?

A cookie is a small file that stores on user's browsers. It is used to store users information. We can create and retrieve cookie values in PHP.

A cookie can be created with the setcookie() function in PHP.



Create Cookie

$cookie_name = "username";

$cookie_value = "Umesh Singh";

setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day

Update Cookie

$cookie_name = "username";

$cookie_value = "Alex Porter";

setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");

Delete Cookie

setcookie("username", "", time() - 3600);

Suggest An Answer

No suggestions avaliable!

Latest post from PHP interview questions

Ask Question