How to use PHP Cookie to store data?

The PHP setcookie() function is used to set or create cookies. Let’s see how to use this function –

The setcookie() function has 7 parameters, but only the name parameter is required, and the others are optional.

setcookie(name, value, expires, path, domain, secure, httponly)
ParameterDescription
name (Required)With the cookie name, you can access the cookie value.
valuevalue of the cookie. The default value is empty.
expiresThe time the cookie expires. If you do not define the cookie will not be removed until the browser cache is cleared.
pathIt defines which pages can access this cookie.
domainDomain-level cookie accessibility. Suppose you have a domain name that is example.com and to make the cookie available on all subdomains of example.com, set the domain to “example.com”. “www.example.com” will make the cookie available only to the www subdomain.
secureIt takes a boolean value (true or false). If it is true the cookie should only be transmitted over a secure HTTPS connection from the client.
httponlyCookies can also be accessed on the client side using JavaScript, but if you pass true to this parameter, the cookie will not be accessible by JavaScript.

    Example of setcookie

    <?php
    $cookie_name = "username";
    $cookie_value = "Rahul Gandhi";
    
    setcookie($cookie_name, $cookie_value);
    Cookie data in browser

    Using the $_COOKIE global variable we can get the cookie value.

    This $_COOKIE variable gives you an associative array of all your cookies, and you can access a specific cookie value using the cookie name. Let’s see –

    <?php
    print_r($_COOKIE);
    Array
    (
        [username] => Rahul
    )
    

    Get value of specific cookie

    <?php
    echo $_COOKIE['username'];
    Rahul

    In the third parameter, you can set the expiry for a cookie, and the time must be in Unix timestamp.

    For the Unix timestamp, we will use the time() function. this function returns the current Unix timestamp.

    Some examples of the time() function

    • time()+60 = after 1 minute.
    • time()+60*2 = after 2 minute.
    • time()+60*60 = after 1 hour (60*60 = 3600 seconds).
    • time()+60*60*24 = after 24 hours or one day.
    • time()+60*60*24*9 = after 9 days.
    <?php
    $cookie_name = "username";
    $cookie_value = "Rahul";
    $expire = time() + 60; // after 60 seconds
    
    setcookie($cookie_name, $cookie_value, $expire);

    Alternative syntax introduced in PHP 7.3.0

    setcookie(name, value, array options[])

    An example of the alternative syntax

    <?php
    $cookie_name = "username";
    $cookie_value = "Rahul";
    $expire = time() + 60;
    
    setcookie($cookie_name, $cookie_value, [
        "expires" => $expire,
        "httponly" => true
    ]);

    The setcookie() functoin will return true if the cookie has been set.

    <?php
    $cookie_name = "username";
    $cookie_value = "Rahul";
    $expire = time() + 60;
    
    $myCookie = setcookie($cookie_name, $cookie_value);
    
    if ($myCookie) {
        echo "The cookie has been sent successfully.";
    } else {
        echo "Oops!, Something is going wrong.";
    }
    The cookie has been sent successfully.