how to logout from a php session - session_destroy doesn't seem to be enough

后端 未结 6 733
囚心锁ツ
囚心锁ツ 2021-01-15 19:01

I\'m trying to learn PHP and using sessions. I seen examples about using session_destroy on logout, but I see this in the php documentation:

In order

相关标签:
6条回答
  • 2021-01-15 19:11

    In my opinion only this is nesessary

    session_start();
    unset($_SESSION["nome"]);  // where $_SESSION["nome"] is your own variable. if you do not have one use only this as follow **session_unset();**
    
    0 讨论(0)
  • 2021-01-15 19:14

    At the first: use the unset() function. So if you have a $_SESSION['user_id'], and you wanne destroy it: unset($_SESSION['user_id'])

    You can also use the unset() for cookies or other vars

    Second: can we have some code?

    0 讨论(0)
  • 2021-01-15 19:29

    There's, like, an example answering your question just below the paragraph you just quoted: http://php.net/manual/en/function.session-destroy.php

    Example #1 Destroying a session with $_SESSION

    <?php
    // Initialize the session.
    // If you are using session_name("something"), don't forget it now!
    session_start();
    
    // Unset all of the session variables.
    $_SESSION = array();
    
    // If it's desired to kill the session, also 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.
    session_destroy();
    ?>
    
    0 讨论(0)
  • 2021-01-15 19:32

    if you are using session id. then you can do this

    session_start();
    unset($_SESSION['id']);
    session_destroy();
    

    for cookies you can set the cookie time to some past date.

    0 讨论(0)
  • 2021-01-15 19:33

    Only use session_unset() for older deprecated code that does not use $_SESSION.

    session_start();

    session_unset($_SESSION['email-user']); session_destroy();`

    0 讨论(0)
  • 2021-01-15 19:35

    I've never actually deleted the session ID from the session. I usually just unset the necessary variables from the session (so if, for instance, you set a logged in user as $_SESSION['user'] = $userModel; then I just unset($_SESSION['user']);). To remove the cookie from the user's browser (like the documentation says) do this:

    setcookie(session_id(), "", time() - 3600);
    

    Read Example #1 from the session_destroy() documentation for more info.

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