Вопрос

I just want to destroy all $_COOKIES info but not the info/ data in $_SESSION, so I use this code below, but it destroy all my $_SESSION data as well which is not what I want,

// Destroy all cookies.
foreach ( $_COOKIE as $key => $value ){
    setcookie( $key, "", time()-1800, '/' );
}

But the $_SESSION data remains undestroyed if I unset the $_COOKIES data one by one, such as,

setcookie('accept_terms_conditions', "", time()-1800, '/');

Why does the foreach code destroy $_SESSION as well. Can I just destroy $_COOKIES only?

Это было полезно?

Решение

When you start a session the PHPSESSID will be added to the $_COOKIE array, which you reset when setting the setcookie using the foreach.

You can try this:

// Destroy all cookies.
foreach ( $_COOKIE as $key => $value ){
    if($key != 'PHPSESSID'){
        setcookie( $key, "", time()-1800, '/' );
    }
}

Другие советы

Session generally (it's php.ini parameter) use cookie. So you have to filter on the cookies you want to destroy to not destroy the one use by the sessions. The cookie name is by default "PHPSESSID", but it may be something else, use session_name() to get it.

foreach ( $_COOKIE as $key => $value ){
    if ( $key != session_name() ) { 
        setcookie( $key, "", time()-1800, '/' );
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top