如果使用PHP登出怎么显示?

问题描述 投票:-1回答:1

所以我有这个index.php文件和另一个level.php文件,登录的用户可以在其中选择级别。如果我想从会话中注销,则希望它使用户回到index.php,但是由于注销不会发送POST数据,因此我无法告诉index.php用户刚刚注销。

我该如何管理它,以便一个小小的div可以通知用户他刚刚成功注销了?

php authentication logout
1个回答
0
投票

登录(通常与客户端和服务器之间的状态保留相关联)的想法通常通过使用cookie(或更恰当的sessions)来解决。这与向服务器发送POST请求无关。只需检查有状态信息仍然有效。

假设您执行了与此类似的操作以使用户登录...

<?php
session_start();
if ($user->signIn()) { // successful sign in attempt
    $_SESSION['signedIn'] = true;
    $_SESSION['userId'] = $user->id;
} else {
    // failed to sign in
}

假设您执行此操作以将用户注销...

<?php
session_start();

// Destroy the session cookie on the client
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}

// Destroy the session data on the server
session_destroy();

然后确定用户是否从index.php登录应该很简单...

<?php
session_start();

if (!empty($_SESSION['signedIn'])) { // They are signed in
    /* Do stuff here for signed in user */
} else { // They are not
    /* Do other stuff here for signed out user */
}
© www.soinside.com 2019 - 2024. All rights reserved.