PHP Cookies 🍪
Cookies in PHP are small pieces of data stored on the user's browser. They are used to store user-related information such as login sessions, preferences, and tracking data across different pages.
How PHP Cookies Work?
1️⃣ A cookie is set using the setcookie()
function.
2️⃣ The cookie is sent to the user's browser and stored.
3️⃣ The server retrieves the cookie when the user revisits the website.
4️⃣ A cookie expires after a specified time or when deleted manually.
Setting a Cookie
A cookie is created using setcookie()
with the following syntax.
setcookie(name, value, expire, path, domain, secure, httponly);
Setting a Cookie
setcookie("user", "John Doe", time() + 3600, "/");
// Cookie "user" is set for 1 hour
Deleting a Cookie
To delete a cookie, set its expiration time to a past time: