PHP - Cookies

The Anatomy of a Cookie

Cookies are small pieces of data that are stored on the user's computer by the web browser while browsing. They are used to remember information about the user, such as their preferences or login status, between sessions. In PHP, cookies can be set and accessed using built-in functions.

PHP - Cookies

What is a Cookie?

A cookie is a text file that a website places on your computer's hard drive. Each time you visit the website, the browser sends the cookie back to the server to notify the website of your previous activity. This allows websites to customize your experience based on your past interactions with them.

Types of Cookies

There are two types of cookies: session cookies and persistent cookies.

  1. Session Cookies: These cookies are temporary and are deleted when the user closes the browser. They are useful for storing temporary information like a shopping cart item list.
  2. Persistent Cookies: These cookies remain on the user's computer until they reach their expiration date or are manually deleted by the user. They are used for longer-term tracking, such as maintaining user preferences or login sessions.

How to Set a Cookie in PHP?

To set a cookie in PHP, you use the setcookie() function. Here's how you can do it:

<?php
// Set a cookie named "username" with a value of "JohnDoe" that expires after 30 days
setcookie("username", "JohnDoe", time() + (86400 * 30), "/");
?>

In this example, we're setting a cookie named "username" with the value "JohnDoe". The third argument is the expiration time, which is calculated by adding the number of seconds in 30 days to the current time (time()). The fourth argument is the path where the cookie is available. By setting it to "/", it means the cookie is available across the entire website.

Accessing Cookies with PHP

Once a cookie has been set, you can access its value using the $_COOKIE superglobal array in PHP. Here's an example of how to retrieve the value of the "username" cookie:

<?php
if(isset($_COOKIE["username"])) {
    echo "Welcome back, " . $_COOKIE["username"] . "!";
} else {
    echo "Welcome, guest!";
}
?>

In this code, we check if the "username" cookie is set using isset(). If it exists, we display a welcome message with the username. Otherwise, we display a generic welcome message for guests.

Deleting the Cookies

To delete a cookie, you need to set its expiration time to a past date. Here's how you can delete the "username" cookie:

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

By setting the expiration time to a time in the past, the browser will automatically remove the cookie. In this example, we're setting the expiration time to one hour ago, effectively deleting the "username" cookie.

That's it! You now know how to set, access, and delete cookies in PHP. Remember, cookies should be used responsibly, as they can store sensitive information. Always ensure that your website follows best practices for cookie management and user privacy.

Happy coding!

Credits: Image by storyset