In PHP, cookies can be created, read, and deleted using the `setcookie()` function.
To create a cookie, use the `setcookie()` function and pass in the name, value, expiration time, and path as parameters. For example:
“`php
setcookie(“username”, “John Doe”, time() + 3600, “/”);
“`
In this example, the cookie named “username” is set to the value “John Doe”. The expiration time is set to one hour from the current time, and the cookie is available to all paths on the server.
To read a cookie, use the `$_COOKIE` superglobal array. The array contains all the cookies that have been set. For example:
“`php
echo $_COOKIE[“username”];
“`
This will output the value of the “username” cookie.
To delete a cookie, set the expiration time to a past time using the `setcookie()` function. For example:
“`php
setcookie(“username”, “”, time() – 3600, “/”);
“`
In this example, the “username” cookie is deleted by setting the expiration time to one hour ago.
It is important to note that cookies are sent in the HTTP header, so you must call the `setcookie()` function before any output is sent to the browser.