简体   繁体   中英

How to completely delete $_COOKIEs in PHP

I have the following PHP script:

foreach( $_COOKIE as $key => $value ) {
  if( strpos( $key, 'ticketRecon_ID-' ) === false ) continue;
  else {
    setcookie( $key, '', time() - 1 );
    unset( $_COOKIE[$key] );
  }
}

When I run the script, print_r($_COOKIE) shows me the targeted cookies have been deleted and do not exist. Yet the Chrome dev tool inspector shows the cookies are still present (see screen shot below) And when I return to the page where the cookies where created they are still present.

How do you completely delete/erase/destroy a _COOKIE in PHP? Might this have any bearing or relation to the site pages using the SSL protocol?

在此处输入图片说明

AMENDED:

Here is how I initially set the cookie:

setcookie( 'ticketRecon_ID-' . $row['reservationID'], 'N', null, '/' );

Solved it.

It seems you must delete a _COOKIE the same exact and identical way you create them. So I was missing the path attribute on my delete setcookie() .

Created like this setcookie( 'ticketRecon_ID-' . $row['reservationID'], 'N', null, '/' );

So delete must be like this setcookie( $key, '', time() - 1, '/' );

Hope this post helps someone someday.

给它分配一个空数组

$_COOKIE = array();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM