Skip to main content

PHP Cookies

By SamK
0
0 recommends
Category(s)
Topic(s)

A cookie serves as a means to identify a user. It is a small file that the server places on the user's computer. Whenever the same computer requests a page via a browser, it sends the cookie along with the request. In PHP, you can easily create and retrieve cookie values, allowing for personalized user experiences and session management.

Create/Retrieve a Cookie

A cookie is created using the setcookie() function.

A cookie value is retrieved using the global $_COOKIE array.

Example:

In this example, we create a cookie named "username" with the value "Jane Smith." The cookie is set to expire in 7 days (60 * 60 * 24 * 7 seconds). By setting the path to /, the cookie will be accessible throughout the entire website. If needed, you can limit the cookie's accessibility to a specific directory by adjusting the path.

To retrieve the value of the "username" cookie, we use the global $_COOKIE array. 

<?php
// Define cookie parameters
$cookie_name = "username";
$cookie_value = "Jane Smith";

// Set the cookie to expire in 7 days
setcookie($cookie_name, $cookie_value, time() + (60 * 60 * 24 * 7), "/"); 

// Check if the cookie is set, and display appropriate message
if (isset($_COOKIE[$cookie_name])) {
  echo htmlspecialchars($_COOKIE[$cookie_name]);
} else {
  echo "No cookie has been set";
}
?>

/*
Output: Jane Smith
*/

Note: The setcookie() function must be called before any HTML content, including the <html> tag. This is because setting cookies involves sending HTTP headers, and headers must be sent before any output is generated by the script.

Note: When a cookie is sent, its value is automatically URL-encoded. When the cookie is retrieved, it is automatically decoded. If you want to prevent URL encoding, you can use the setrawcookie() function instead.

Modify a Cookie Value

To update or modify a cookie, simply call the setcookie() function again with the new value.

<?php
// Define the cookie name and the new value
$cookie_name = "username";
$cookie_value = "Julia Adams"; // From Jane Smith to Julia Adams

setcookie($cookie_name, $cookie_value, time() + (60 * 60 * 24 * 7), "/");

if (isset($_COOKIE[$cookie_name])) {
  echo htmlspecialchars($_COOKIE[$cookie_name]);
} else {
  echo "No cookie has been set";
}
?>

/*
Output: Julia Adams
*/

Delete a Cookie

To delete a cookie, call the setcookie() function and set its expiration date to a time in the past.

<?php
// Delete the cookie by setting its expiration time to one hour ago
setcookie("username", "", time() - 3600, "/"); 

if (isset($_COOKIE[$cookie_name])) {
  echo htmlspecialchars($_COOKIE[$cookie_name]);
} else {
  echo "The cookie has been deleted.";
}
?>

/*
Output: The cookie has been deleted.
*/

Check if Cookies are Enabled

To check if cookies are enabled in the user's browser, create a test cookie using the setcookie() function. Then check the number of elements in the $_COOKIE array, by using the count() function. If the number is greater than 0, it means that cookies are enabled.

<?php
// Attempt to set a test cookie that expired 1 hour ago
setcookie("test_cookie", "test_value", time() - 3600, '/');

// Check if the $_COOKIE array is empty or not
if (count($_COOKIE) > 0) {
    echo "Cookies are enabled.";
} else {
    echo "Cookies are disabled.";
}
?>

/*
Output: Cookies are disabled.
*/

Questions & Answers