PHP | Cookies

What are Cookies?

A cookie is a small file with the maximum size of 4KB that the web server stores on the client computer.They are typically used to keeping track of information such as a username that the site can retrieve to personalize the page when the user visits the website next time.A cookie can only be read from the domain that it has been issued from.Cookies are usually set in an HTTP header but JavaScript can also set a cookie directly on a browser.

Setting Cookie In PHP: To set a cookie in PHP,the setcookie() function is used.The setcookie() function needs to be called prior to any output generated by the script otherwise the cookie will not be set.

Syntax :

setcookie(name, value, expire, path, domain, security);

Parameters: The setcookie() function requires six arguments in general which are:

  1. Name: It is used to set the name of the cookie.
  2. Value: It is used to set the value of the cookie.
  3. Expire: It is used to set the expiry timestamp of the cookie after which the cookie can’t be accessed.
  4. Path: It is used to specify the path on the server for which the cookie will be available.
  5. Domain: It is used to specify the domain for which the cookie is available.
  6. Security: It is used to indicate that the cookie should be sent only if a secure HTTPS connection exists.

Below are some operations that can be performed on Cookies in PHP:

  • Creating Cookies: Creating a cookie named Auction_Item and assigning the value Luxury Car to it.The cookie will expire after 2 days(2 days * 24 hours * 60 mins * 60 seconds).

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    setcookie("Auction_Item", "Luxury Car", time()+2*24*60*60);
      
    ?>

    chevron_right

    
    

    Note: Only the name argument in the setcookie() function is mandatory.To skip an argument,the argument can be replaced by an empty string(“”).

  • Checking Whether a Cookie Is Set Or Not: It is always advisable to check whether a cookie is set or not before accessing its value.Therefore to check whether a cookie is set or not, the PHP isset() function is used.

    To check whether a cookie “Auction_Item” is set or not,the isset() function is executed as follows:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    if(isset($_COOKIE["Auction_Item"])){
        echo "Auction Item is a  " . $_COOKIE["Auction_Item"];
    } else{
        echo "No items for auction.";
    }
      
    ?>

    chevron_right

    
    

    Output:

    Auction Item is a Luxury Car.
  • Accessing Cookie Values: For accessing a cookie value, the PHP $_COOKIE superglobal variable is used.It is an associative array that contains a record of all the cookies values sent by the browser in the current request.The records are stored as a list where cookie name is used as the key.

    To access a cookie named “Auction_Item”,the following code can be executed:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    echo "Auction Item is a  " . $_COOKIE["Auction_Item"];
      
    ?>

    chevron_right

    
    

    Output:

    Auction Item is a Luxury Car.
  • Deleting Cookies: The setcookie() function can be used to delete a cookie.For deleting a cookie, the setcookie() function is called by passing the cookie name and other arguments or empty strings but however this time, the expiration date is required to be set in the past.

    To delete a cookie named “Auction_Item”,the following code can be executed:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    setcookie("Auction_Item", "", time()-60);
      
    ?>

    chevron_right

    
    

Important Points

  1. If the expiration time of the cookie is set to 0, or omitted, the cookie will expire at the end of the session i.e. when the browser closes.
  2. The same path, domain, and other arguments should be passed that were used to create the cookie in order to ensure that the correct cookie is deleted.



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.