PHP Cookies

PHP Cookies are small pieces of data that can be stored on the computer of the user in order to track his or her browsing behavior.

Cookie files are used in order to store information about users, such as the user’s login credentials or the contents of their shopping carts.

Besides tracking user behavior, they can also be used to gather information about the pages and products that a user is visiting on the site.

The purpose of this article is to describe some of the basic methods for setting and managing cookies in PHP, such as defining and retrieving cookie values, deleting cookies, and handling cookie security issues, and to go over just some of these methods.



A cookie is a piece of information that is used to identify a particular user. Basically, cookies are just small files that a server embeds on the back of a user’s computer when they visit it.

The cookie will be sent every time a computer requests a page through a browser, and it will be sent each time a page is requested.

Using PHP, you can both create cookies as well as retrieve the values stored in them.


PHP Cookies Creation

Using the setcookie() function, you can create a cookie that will be stored on your computer.

Syntax

setcookie(name, value, expire, path, domain, secure, httponly);

There is only one parameter required, which is the name parameter and rest of the parameters are optional.

below example checks if a cookie named $c_name is set. If it is set, it displays its value to the user, and if it is not set, it informs the user that the cookie is not set.

Php Cookie Example: 1 

<!DOCTYPE html> <?php $c_name = "Programminglanguages"; $c_value = "PHP"; setcookie($c_name, $c_value, time() + (86400 * 15), "/"); // 86400 = 1 day ?> <html> <body><?php if(!isset($_COOKIE[$c_name])) { echo "Cookie named '" . $c_name . "' is not set!"; } else { echo "Cookie '" . $c_name . "' is set!<br>"; echo "Value is: " . $_COOKIE[$c_name]; } ?></body> </html>
In the example below, we are looking at how to set values directly in the setcookie() function:

Php Cookie Example: 2 

<!DOCTYPE html> <?php setcookie("user_name", "Jeff Bezos", time()+3600, "/","", 0); setcookie("user_age", "59", time()+3600, "/", "", 0); ?> <html> <body><?php if( isset($_COOKIE["user_name"])){ echo "Hello " . $_COOKIE["user_name"] . " "; } else{ echo "Not recognized" . " "; } ?></body> </html>
Remember: It is important that the setcookie() function appears before the <html> tag.

When the cookie is sent, it will be automatically encoded with URL encoding, and when it is received, it will automatically decode it with the same encoding (to prevent URLencoding, make sure you use setrawcookie() instead).


The following are the steps that you can follow if you wish to modify a cookie value in PHP:

  1. Using the $_COOKIE superglobal array, you can retrieve the value of an existing cookie.
  2. As needed, make adjustments to the value.
  3. The setcookie() function must be used again with the modified value in order to set the cookie again.

Here, we are using above example 1 to modify data in cookie:

Example: 

<!DOCTYPE html> <?php $c_name = "Programminglanguages"; $c_value = "JAVA"; setcookie($c_name, $c_value, time() + (86400 * 15), "/"); // 86400 = 1 day ?> <html> <body><?php if(!isset($_COOKIE[$c_name])) { echo "Cookie named '" . $c_name . "' is not set!"; } else { echo "Cookie '" . $c_name . "' is set!<br>"; echo "Value is: " . $_COOKIE[$c_name]; } ?></body> </html>
In the example below, we are updating the above example 2 by using the setcookie() function:.

Example: 

<!DOCTYPE html> <?php setcookie("user_name", "Andy Jassy", time()+3600, "/","", 0); setcookie("user_age", "55", time()+3600, "/", "", 0); ?> <html> <body><?php if( isset($_COOKIE["user_name"])){ echo "Hello " . $_COOKIE["user_name"] . " "; } else{ echo "Not recognized" . " "; } ?></body> </html>

Using the setcookie() function in PHP you can delete a cookie by setting its expiration time to a past time, following which the cookie will be deleted.

Here is an example of how to delete a cookie:

Php Cookie Delete Example: 1 

<?php setcookie("Programminglanguages", "", time() – 3600);// set the expiration date to one hour ago ?> <html> <body> <?php echo "Cookie 'Programminglanguages' is deleted."; ?> </body> </html>

Here is another example of deleting cookies from the above example 2:

Php Cookie Delete Example: 2 

<?php // set the expiration date to one hour ago setcookie("user_name", "", time() – 3600); setcookie("user_age", "", time() – 3600); ?> <html> <body> <?php echo "Cookie 'user_name' and 'user_age' is deleted."; ?> </body> </html>

Ensure cookies are enabled

Here is an example of a small script that is used to check if cookies have been enabled in your browser.
Let us start by creating a test cookie using the setcookie() function, and then count the array variables in the $_COOKIE variable, as shown below:

Example: 

<?php setcookie("cookie_testing", "cookiee test", time() + 3600, '/'); ?> <html><body> <?php if (count($_COOKIE) > 0) { echo "A cookie is enabled on your browser."; } else { echo "A cookie is disabled on your browser."; } ?> </body> </html>
If you found this article enjoyable, you can subscribe using the form below to receive the latest updates in PHP.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *