what is a cookie in php

In this article we will see, in detail, what cookies are and how to make the most of them, using one of the most popular languages: PHP.

What is a cookie

A cookie is nothing more than a small text file that allows you to store a small amount of data (approximately 4 KB) on the user’s computer. They are typically used to keep track of information such as the username that the site can retrieve to personalize the page when the user visits the site for the next time.

Therefore, every time the browser makes a new request to the server, all cookie data is automatically sent to the server within the request itself.

How to set a cookie in PHP

The setcookie () function is used to set and create a cookie in PHP. Make sure you call this function before generating or outputting your script otherwise the cookie will not be set. The basic syntax of this function is:

setcookie ( name , value , expires , path , domain , secure );

The setcookie () parameters can be summarized as follows:

ParameterDescription
nameThe name of the cookie.
ValueThe value of the cookie. Do not store sensitive information as this value is stored in the computer of the user, and consequently this data could be read by malicious people.
expiresThe expiration date in UNIX timestamp format. After this time the cookies will become inaccessible, i.e. the value of a cookie will no longer be readable. Therefore its default value will be 0.
PathSpecify the path on the server for which the cookie will be available. If set to /, the cookie will be available within the entire domain. Therefore, each script, present within the domain, will be able to retrieve and exploit the value of the cookie.
DomainSpecify the domain for which the cookie is available, for example www.example.com.
SecureThis field, if present, indicates that the cookie should only be sent if a connection exists Secure HTTPS.
Parameters function setcookie

Please note that if the cookie expiration date is omitted or set to 0, the cookie will expire at the end of the session, i.e. when the browser is closed.

Here is an example that uses the setcookie () function to create a cookie named username and assign it the value John Carter. It is also specified that the cookie will expire in 30 days (30 days * 24 hours * 60 min * 60 sec).

Example:

<?php
   // ----------------
   // Setting a cookie
   // ----------------
   setcookie("username", "John Carter", time()+30*24*60*60);
?>

; ?>

The PHP code from the previous example produces the following output:

John Carter

It is a good idea to always check if a cookie is set or not before accessing its value. To do this you can use PHP’s isset () function:

Example:

<?php
   // ----------------------------------------
   // Verifying whether a cookie is set or not
   // ----------------------------------------
   if(isset($_COOKIE["username"])){
      echo "Hi" . $_COOKIE["username"];
   } else{
      echo "Welcome…!";
   }
?>

You can also use PHP’s print_r () function like this: print_r ($ _ COOKIE); to see the $_COOKIE associative array structure, just like you do with other types of arrays.


[adinserter name=”Block 1″

Removal of cookie

You can delete a cookie by always invoking the setcookie () function with the cookie name and any value (such as an empty string, for example), however this time you need to set the expiration date to the past, as shown in the following example:

Example:

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

To make sure that the cookie is deleted correctly, you should go to the setcookie function, exactly the same parameters that you used when you created the cookie.

Fonte: https://www.tutorialrepublic.com/php-tutorial/php-cookies.php

Seguimi su
Latest posts by Antonio Lamorgese (see all)

Antonio Lamorgese

Network administrator, web developer and expert in SEO practices. After years of industry experience, I have created a PHP code generator. Find out more at https://www.phpcodewizard.it.