Working with Cookies in PHP

In PHP, cookies can be set using the setcookie() function and then accessed and manipulated using the $_COOKIE superglobal array.

Set cookie:

setcookie("username", "john_doe", time() + 3600, "/");

Retrieve the value of the cookie:

$cookieValue = $_COOKIE["username"];

Remove cookies:

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

Note: There should be no output, including white spaces, before using the setcookie() function to set a cookie.

bannerAds