如果您在 PHP 中注销,如何显示?



所以我有这个索引.php文件和另一个级别.php文件,记录用户可以在其中选择级别。如果我想从会话注销,我希望它将用户带回索引.php,但由于注销不发送 POST 数据,我无法告诉索引.php用户刚刚注销。

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

登录的想法(通常与客户端和服务器之间的状态保留相关联(最常通过使用 cookie(或更恰当的会话(来解决。这与向服务器发送 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();

然后确定用户是否从索引登录.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 */
}

最新更新