How to Set Cookie in PHP

2 years ago admin PHP

In this tutorial, we will see how to set a cookie, how to retrieve a cookie, how to update a cookie, and how to delete a cookie in PHP.


Set and retrieve a cookie in php

To set a cookie in PHP use the code below:

                                                    
                                                                                                                
<?php
    $name = "current_user";
    $value = "John Doe";
    setcookie($name, $value, time() + (3600 * 10), "/");// 3600 * 10 = 10 hours
?>
<html>
    <body>
        <?php
            if(isset($_COOKIE[$name])) {
                echo "Name: " . $name . "<br>";
                echo "Value: " . $_COOKIE[$name];
            }else{
                echo "Cookie not set yet";
            }
        ?>
    </body>
</html>

Update a cookie in php

To update a cookie in PHP we use the same code just change the value:

                                                        
                                                                                                                        
<?php
    $name = "current_user";
    $value = "Jack Jock";
    setcookie($name, $value, time() + (3600 * 10), "/");// 3600 * 10 = 10 hours
?>
<html>
    <body>
        <?php
            if(isset($_COOKIE[$name])) {
                echo "Name: " . $name . "<br>";
                echo "Value: " . $_COOKIE[$name];
            }else{
                echo "Cookie not set yet";
            }
        ?>
    </body>
</html>

Delete a cookie in php

To delete a cookie we set the expiration date to one hour ago: 

                                                        
                                                                                                                        
<?php
    setcookie("current_user", "", time() - 3600);
?>
<html>
    <body>
        <?php
            echo "Cookie is deleted.";
        ?>
    </body>
</html>

Related Tuorials

How to Handle Forms with PHP (GET and POST Methods Explained)

When building dynamic websites using PHP, you need to get and process the user input data. Here come...


How to Create and Use Functions in PHP (With Examples for Beginners)

In PHP, a function is a block of code that is executed to perform a specific task. This is a go...


How to Use Variables and Data Types in PHP (A Clear Guide for Absolute Beginners) Part 2

In the second part of this guide, we will see more PHP types.Also, we will see how to check a variab...


How to Use Variables and Data Types in PHP (A Clear Guide for Absolute Beginners) Part 1

The first thing you need to understand when learning PHP is variables and data types. In this g...


How to Install and Run PHP on Your Localhost (Step-by-Step Guide)

If you want to learn PHP or build a web application with it, the first step is to create a working l...


CRUD Application with PHP PDO Ajax, and MySQL Part 2

In the second part of this tutorial, we will get all the students from the database and display them...


CRUD Application with PHP PDO Ajax, and MySQL Part 1

In this tutorial we will see how to create a crud application with PHP PDO Ajax, and MySQL, the user...


How to Sort Associative Arrays in Descending Order According to the Key Value in PHP

in this lesson, we will see how to sort associative arrays in descending order according to the key...


How to Sort Associative Arrays in Ascending Order According to the Key Value in PHP

in this lesson, we will see how to sort associative arrays in ascending order according to the key v...


How to Sort Associative Arrays in Descending Order According to the Value in PHP

in this lesson, we will see how to sort associative arrays in descending order according to the valu...