代码之家  ›  专栏  ›  技术社区  ›  Shiv Deepak

在PHP中从会话注销的正确方法

  •  30
  • Shiv Deepak  · 技术社区  · 15 年前

    脚本1

    <?php
    session_start();
    session_destroy();
    header("location:index.php");
    ?>
    

    脚本2

    <?php
    session_start();
    session_unset();
    session_destroy();
    header("location:index.php");
    ?>
    

    脚本3

    <?php
    session_start();
    if (isset($_SESSION['username']))
    {
        unset($_SESSION['username']);
    }
    header("location:index.php");
    ?>
    

    4 回复  |  直到 15 年前
        1
  •  68
  •   Frxstrem    15 年前

    session_destroy() PHP manual :

    <?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();
    ?>
    
        2
  •  13
  •   ircmaxell    15 年前

    session_start();
    setcookie(session_name(), '', 100);
    session_unset();
    session_destroy();
    $_SESSION = array();
    

    这样,它将终止cookie,销毁所有存储在内部的数据,并销毁会话信息的当前实例(被忽略) session_destroy ).

        3
  •  5
  •   Haim Evgi    15 年前

    Session_unset(); session_destroy(); 这也会破坏会话。

    setcookie() 可能是用来做这个的

        4
  •  3
  •   Optimaz Prime    7 年前
    <?php
    // Initialize the session.
    session_start();
    // Unset all of the session variables.
    unset($_SESSION['username']);
    // Finally, destroy the session.    
    session_destroy();
    
    // Include URL for Login page to login again.
    header("Location: login.php");
    exit;
    ?>
    
    推荐文章