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.

Syntax

setcookie(name, value, expire, path, domain, secure);

Example

<?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.

Example

<?php
    // Accessing an individual cookie value
    echo $_COOKIE["name"];
?>

Removing Cookies

PHP Delete a cookie use the setcookie() function with an expiration date in the past.

Example

<?php
    // Deleting a cookie
    setcookie("username", "", time()-3600);
?>