How do I kill a PHP session?

后端 未结 2 1800
渐次进展
渐次进展 2020-12-12 01:22

I am writing a social networking site, and I am trying to figure out PHP sessions. At the top of the login page, I call session_destroy(), and I call session_start() at the

相关标签:
2条回答
  • 2020-12-12 01:43

    The best way is by following the manual. Here is sample code that erases any session variables, the session cookie and then the session file itself:

    <?php
    
    // Unset all of the session variables.
    $_SESSION = array();
    
    // Delete the session cookie.
    // Note: This will destroy the session, and not just the session data!
    if( ini_get( "session.use_cookies" ) ) {
        $params = session_get_cookie_params();
    
        setcookie(
          session_name()
          , ''
          , time() - 42000
          , $params[ "path"     ]
          , $params[ "domain"   ]
          , $params[ "secure"   ]
          , $params[ "httponly" ]
        );
    }
    
    // Finally, destroy the session.
    if( session_status() === PHP_SESSION_ACTIVE ) { session_destroy(); }
    
    0 讨论(0)
  • 2020-12-12 01:45

    Use session_destroy to destroy the session data and session_unset to clear the $_SESSION variable respectively.

    Furthermore, call session_regenerate_id(true) after an authentication attempt to change the current session’s ID and destroy the session data that is still associated to the old session ID.

    0 讨论(0)
提交回复
热议问题