How do you create and use cookies in PHP

Cookies are small pieces of data that are stored on the user's computer by the web browser while browsing a website. PHP makes it easy to create and manage cookies for user sessions, preferences, and tracking purposes.

PHP Cookies, Set Cookies, Retrieve Cookies, Manage Cookies, Web Development

This guide explains how to create and use cookies in PHP, including how to set, retrieve, and delete cookies.

Example of Creating and Using Cookies in PHP

<?php // Set a cookie named "user" with the value "John Doe" that expires in one hour setcookie("user", "John Doe", time() + 3600, "/"); // Check if the cookie is set and display its value if(isset($_COOKIE["user"])) { echo "User is: " . $_COOKIE["user"]; } else { echo "User cookie is not set."; } // To delete a cookie, you can set it to an expired time // setcookie("user", "", time() - 3600, "/"); ?>

PHP Cookies Set Cookies Retrieve Cookies Manage Cookies Web Development