Where are cookies stored php?

The cookie is used to store some information in the user’s computer by the web browser when the user visits a website. When a user sends a request for a web page on a website using a browser, the server sends the set-cookie HTTP headers with the response that will request the browser to create the cookie file in the user’s computer. If the cookie is disabled in the browser, no cookie information will be stored in the user’s computer. The cookie information can be accessed in PHP by using a PHP superglobal variable named $_COOKIE. The purpose of using cookies and the ways to set cookie data using PHP script are shown in this tutorial.

setcookie() Function

The setcookie() function is used to define the cookie that will be sent with the HTTP header. The syntax of this function is provided below:

The first argument contains the cookie name.

The second argument contains the cookie value.

The third argument contains an array of cookie options. The values of the array can contain the values of expires, path, domain, secure, and httponly. The expires contains the time limit for the existing cookie. The path defines the server path where the cookie information will be stored. The domain contains the value where the cookie will be available. The secure indicates the cookie information will be transmitted over the HTTPS connection. The value of the httponly is set to true to confirm that the cookie data will be accessible through the HTTP protocol. The options associative array can contain one or more values mentioned here. The $_COOKIE variable is used to read the cookie values.

It returns true upon success and returns false upon failure.

Different uses of the cookie have been shown in this part of the tutorial by using multiple examples:

Example 1: Set a Simple Cookie Value
Create a PHP file with the following script to set a cookie value based on the value submitted by the form. The HTML form is used to take the username from the user and create a cookie based on the form data. The cookie will expire when the user will close the browser:

<?php
//Check the cookie variable is set or not
if(isset($_COOKIE["username"])) {
    //Print the current cookie value
    echo "<br/>The cookie value is set for : " . $_COOKIE["username"];
    exit();
}

//Check the submit button is clicked or not
if(isset($_GET['sub']))
{
    //Check the username field is empty or not
    if(isset($_GET['user']))
    {
        //Set the username
        $username = $_GET['user'];
        //Set the cookie value for the username
        setcookie("username",$username);
        //Print the current cookie value
        echo "<br/>The cookie value is set for : " . $username;
        }
    }
    else
    {
        //Display message for not getting the cookie data
        echo "No cookie information is found.";
?>

<form action = "#" method = "get">
    Enter username: <input type = "text", name = "user" />
    <input type = "submit" name = "sub" value = "Set Cookie" />
</form>

<?php
    }
?>

Output:
The following output will appear after executing the previous script for the first time in the browser:

Where are cookies stored php?

The following output will appear if the user refreshes the page after submitting the value “fahmida” by using the form:

Where are cookies stored php?

Example 2: Set a Simple Cookie With Expire Date
Create a PHP file with the following script to set a cookie value based on the value submitted by the form with the cookie’s expire time. The HTML form is used to take the name from the user and create a cookie based on the name submitted by the form data. According to the script, the cookie will expire after 4,200 seconds or 7 hours:

<?php
    //Check the cookie variable is set or not
    if(isset($_COOKIE["name"]))
    {
        //Print the cookie name
        echo "<br/>The cookie value is : ".$_COOKIE["name"]."<br/>";
        //Print the cookie expire date
        echo "The cookie expire date is :  ".date('d-m-Y', time()+4200);
        exit();
        }

    //Check the submit button is clicked or not
    if(isset($_GET['sub']))
    {
        //Check the username field is empty or not
        if(isset($_GET['name']))
        {
            //Set the username
            $name = $_GET['name'];
            //Set the cookie with multiple options
            setcookie("name", $name, time()+4200, "/","", 0);
            echo "<br/>The cookie value is set for : " . $name;
        }
    }
    else
    {
        //Display a message for not getting the cookie data
    echo "No cookie information is found.";
    ?>

<form action="#" method="get">
    Enter your name:
    <input type="text" , name="name" />
    <input type="submit" name="sub" value="Set Cookie" />
    </form>
    <?php
    }
?>

Output:
The following output will appear after executing the previous script for the first time in the browser:

Where are cookies stored php?

The following output will appear if the user refreshes the page after submitting the value, “Fahmida Yesmin” by using the form:

Where are cookies stored php?

Example 3: Delete Existing Cookie Values
The most simple way to delete a cookie is to set the cookie expiration time in the past. Create a PHP file with the following script to know the way of deleting a cookie. The existing cookie name will be required to use in the setcookie() function with the past expiration time to delete an existing cookie. Here, the cookie for the username, “fahmida”, will be deleted if it exists:

<?php
//Check the cookie exists or not
if(isset($_COOKIE['username']))
{
    //Check the cookie value exists or not
    if($_COOKIE['username'] == 'fahmida')
    {
        //Delete the cookie
        setcookie ("username", "fahmida", time() - 3600);
        //Print the conformation message
        echo "Cookie information has deleted for 'fahmida'";
    }
}
else
    echo "Cookie information does not exist.";
?>

Output:
The following output will appear after executing the previous script.

Where are cookies stored php?

Conclusion

The purpose of using the cookie and the ways to create, read, and delete the cookie in PHP have been explained in this tutorial by using different examples. I hope, the PHP users will be able to use the cookie in PHP properly after reading this tutorial. Check out Linux Hint for more tips and information.

About the author

Where are cookies stored php?

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Where do cookies get stored?

If you're wondering “where are cookies stored,” it's simple: your web browser will store it locally to remember the “name-value pair” that identifies you. If a user returns to that site in the future, the web browser returns that data to the web server in the form of a cookie.

What are cookies how they are created and stored in PHP?

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.
PHP cookie is a small piece of information which is stored at client browser. It is used to recognize the user. Cookie is created at server side and saved to client browser. Each time when client sends request to the server, cookie is embedded with request.

How can I tell if PHP cookies are created?

PHP Cookies Checking if a Cookie is Set Use the isset() function upon the superglobal $_COOKIE variable to check if a cookie is set.