문제

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