Managing Cookies in PHP

Andrei Birta
3 min readJan 25, 2023

--

Cookies are small text files that are stored on the client’s computer by the web server. They can be used to store various types of information, such as user preferences, login information, and shopping cart contents. In PHP, cookies are created and accessed using the setcookie()and $_COOKIEsuperglobal variables, respectively.

An example of how to create a cookie in PHP:

<?php
setcookie("username", "JohnDoe", time() + 3600);
?>

In this example, the setcookie() function is used to create a cookie named “username” with a value of “JohnDoe” and a lifespan of 3600 seconds (1 hour). The time()function is used to get the current timestamp, which then adds to the desired lifespan to set the expiration date of the cookie.
To access the value of a cookie, you can use the $_COOKIEsuperglobal variable like this:

<?php
echo $_COOKIE["username"];
?>

This will output the value of the “username” cookie, which is “JohnDoe” in this example.

It’s also possible to set additional parameters when creating a cookie, such as the path and domain for which the cookie is valid, and whether the cookie should be sent over a secure connection.
Here’s an example of how to set these additional parameters:

<?php
setcookie("username", "JohnDoe", time() + 3600, "/", "example.com", true, true);
?>

In this example, the cookie is valid for the “/” path on the “example.com” domain, and it is also set to be sent over a secure connection using the “httponly” flag, which means that the cookie will only be accessible through the HTTP protocol and not through scripting languages such as javascript.

You can also delete a cookie by setting its expiration date to a time in the past using the setcookie() function:

<?php
setcookie("username", "", time() - 3600);
?>

This will set the expiration date of the “username” cookie to 1 hour in the past, effectively deleting it.

Cookies can be useful for storing small amounts of information on the client’s computer, such as user preferences or login information. However, it’s important to keep in mind that cookies are stored on the client’s computer, so their contents can be modified or deleted by the user. Additionally, because cookies are sent with every request to the server, they can increase the amount of data sent over the network, which can affect the performance of your website. Therefore, it’s important to use cookies judiciously and to consider alternative storage methods, such as server-side sessions, for sensitive or critical information.

In conclusion, cookies are small text files that are stored on the client’s computer by the web server. They can be used to store various types of information, such as user preferences, login information, and shopping cart contents. PHP provides setcookie() function to create and setcookie() function to delete the cookie. Also, PHP provides $_COOKIEsuperglobal variable to access the value of a cookie.

Get more valuable insights and tips by following me!
Also check my list of articles:
- Basics in PHP
- Different type of architecture
- PHP Interesting Stuff

--

--