What is a Cookies
A cookie is a small text file that lets you store a small amount of data on the user's computer. Cookies are typically used to keeping track of information such as username that the site can retrieve to personalize the page when user visit the website next time.
Create Cookies in PHP
PHP provided setcookie() function to set a cookie.
setcookie(name, value, expire, path, domain, secure);
<?php // Set a cookie setcookie("name", "abc", time() + (86400 * 30), "/"); // 86400 = 1 day ?>
Accessing Cookies in PHP
PHP $_COOKIE is a super global variable is used to retrieve a cookie value.
<?php // Accessing an individual cookie value echo $_COOKIE["name"]; ?>
PHP Delete a cookie use the setcookie() function with an expiration date in the past.
<?php // Deleting a cookie setcookie("username", "", time()-3600); ?>