Change language

Delete cookies with PHP

To create cookies, you can set cookies using the setcookie() functionPHP.Syntax:
setcookie (name, value, expire, path, domain, secure, httponly)
Parameters:this function takes seven parameters as above and described below:
  • name:the name of the cookie.
  • value:the value you want to store in the cookie.
  • expire-time:the number of seconds during which the cookie will be stored on the user’s computer by the browser. After that, it will be automatically deleted. If not set, the cookie will be kept by the browser until it is opened.
  • path:defines which directories the cookie will be valid for. If you want to access it in all directories, then put it in "/", that is, the cookie is available in the entire domain. Otherwise, the cookie will be limited to a subdirectory.
  • domain:is used to define the hierarchy of cookie access. For example, if you set this parameter to "yourdomain.com", it will be available across all subdomains. but if it is set to "sub.yourdomain.com" it will be accessible to "sub.yourdomain.com" and its subdomains.
  • secure:it defines how cookies will be sent over HTTP or HTTPS. If set to true then the cookie will only be sent over HTTPS, otherwise it will be sent over HTTP. The default is false .
  • httponly:If set to true, the cookie is only available over HTTP or HTTPS. This means that client code (such as Javascript) cannot access the cookie.
Of the above parameters, only the first twoparameters are required. Others are optional parameters. If you want to save cookies, specify the expire-time parameter.Note.It is stored in a global array named $_COOKIE .Creating cookies:As mentioned earlier, we can set cookies using the setcookie()function.
  • Example :  
    $cookie_name = "gfg" ; $cookie_value = "GeeksforGeeks" ;  
    // 86400 = 1 day setcookie ( $cookie_name , $cookie_value , time() + (86400 * 15), " / " ); ?>  

    if (! isset ( $_ COOKIE [ $cookie_name ])) { echo "Cookie named’ " . $cookie_name . "’ is not set! " ; } else { echo "Cookie’ " . $cookie_name . "’ is set!
    "
    ; echo "Value is:" . $_ COOKIE [ $cookie_name ]; } ?>  
     
    < / html >
  • Exit:
    Cookie ’gfg’ is set! Value is: GeeksforGeeks
Deleting Cookies:There is no dedicated special function in PHP for deleting cookies. All we need to do is update the cookie’s expiry timeto the past using the setcookie()function. A very easy way to do this - subtract a few seconds from the current time.
  • Syntax:
    setcookie (name, time() - 3600);
  • Example :
     
    // Set the expiration date back one hour setcookie ( "gfg" , "" , time() - 3600); ?>  
     
    echo "Cookie’ gfg’ is deleted. " ; ?>  
     
    < / html >
  • Exit:
    Cookie ’gfg’ is deleted.
Note.The setcookie() function must appear before the tag.

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method