Open In App

How to create and destroy cookies in PHP ?

Improve
Improve
Like Article
Like
Save
Share
Report

Cookies are used to store the user information in the browser. If you store the cookie in the browser then every time you logged in to the browser then you can log in with the help of stored user information. We can understand this by saying when a user sends the request to the server then the cookie tells that this user is already logged in your browser and the server recognizes the user and allows the user to log in.

Now we understand how a cookie works in PHP by the following example.

Example 1: You can create the cookies by writing setcookie() and entering the expiry date of the cookie. If you want to delete the cookie then set the cookie expiry date to the current time.

If you want to display the cookie then you can echo the cookie by $_cookie[‘name’]  and it will print the cookie details.

PHP




<html>
 
<?php
 
    // Checking if create cookie button is set or not
    // and if it is set then creating new cookies
    if(isset($_POST["Submit1"])) {
        setcookie("name",$_POST["name"], time() + 3600, "/", "", 0);
        setcookie("age", $_POST["age"], time() + 3600, "/", "", 0);
        setcookie("city", $_POST["city"], time() + 3600, "/", "", 0);
        echo "<center><h4>Your Cookies are now Created...</h4></center>";
    }
     
    // Checking delete cookie button is set or not
    // And if it is set then deleting the cookies
    if(isset($_POST["Submit3"])) {
        setcookie("name","", time() - 3600, "/", "", 0);
        setcookie("age", "", time() - 3600, "/", "", 0);
        setcookie("city", "", time() - 3600, "/", "", 0);
        echo "<center><h4>Your Cookies are now Deleted...</h4></center>";
    }
?>
 
<head>   
    <meta charset="utf-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1">
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
</head>
 
<body style="background:#FADBD8 ">
    <center>
        <div class="container ">
            <h2 style="color:green">GeeksforGeeks</h2>
            <strong>Cookie portal</strong><br/><br/>
            <form method="POST" action="">
 
                <div class="form-group">Enter Your Name:
                     <input class="form-control" type="text"
                           name="name" style="width:500;">
                </div>
                <div class="form-group">Enter Your Age:
                     <input class="form-control" type="text"
                           name="age" style="width:500;"></div>
                <div class="form-group">Enter Your City:
                     <input class="form-control" type="text"
                           name="city" style="width:500;"></div>
                <br/>
                <input type="submit" name="Submit1" value="Create Cookie"
                       style="width:150;margin-left:10px;">
                <input type="submit" name="Submit2" value="Retrieve Cookie"
                       style="width:150;">
                <input type="submit" name="Submit3" value="Delete Cookie"
                       style="width:150;">
            </form>
        </div>
 
        <?php
 
        // Checking if retrieve cookie button is set or not
        // and if it is set and cookies are also stored
        // then printing the cookies data otherwise if
        // cookies are deleted then printing the message
        // that cookies are deleted
        if(isset($_POST['Submit2'])) {
         
            if(isset($_COOKIE["name"])) {
 
                // Printing the cookie data
                echo "Name = ". $_COOKIE["name"]."<br/>";
                echo "Age = ". $_COOKIE["age"]."<br/>";
                echo "City = ". $_COOKIE["city"]."<br/>";
            }
            else
            {
                echo "<center><h4>Sorry can't retrieve..
                   Your Cookies are deleted !!</h4></center>";
            }
        }
        ?>
    </center>
</body>
</html>


 

 

Output:

 

 

You can see the cookies that are created in the browser by inspecting the browser. First, press the F12 key or click the right button and then select inspect after that you can select an application. You can see there is storage, in that there are cookies section just select that and you can see the below image.

 

Application panel of the browser

Application panel of the browser

 



Last Updated : 10 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads