How to Set Cookie in PHP

1 year 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 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...


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

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


How Do you Sort an Array in Descending Order in PHP

In this lesson, we will see how to sort descending an array in PHP, we will use the rsort() function...


How to Sort Ascending an Array in PHP

In this lesson, we will see how to sort ascending an array in PHP, we will use the sort() function t...


How to Remove a Key and its Value from an Associative Array in PHP

In this lesson, we will see how to remove a key and its value from an associative array in PHP,&nbsp...


How to Modify a Value in an Associative Array in PHP

In this lesson, we will see how to modify a value in an associative array in PHP, an Associative arr...


How to Add an Item to an Associative Array in PHP

In this lesson, we will see how to add an item to an associative array in PHP, an Associative array...


How to Add an Element to an Array in PHP

In this lesson, we will see how to add an element to an array in PHP, PHP has a built-in function th...