我的问题似乎很简单,但我不知道我的代码有什么问题。我有一个非常简单的登录系统,如下所示:
的login.php:
<?php
session_start();
if ($_SESSION['loggedin'] = 1) {
header("Location: admin.php");
}
if ($_GET['login']) {
// Only load the code below if the GET
// variable 'login' is set. You will
// set this when you submit the form
if ($_POST['username'] == 'thenemis'
&& $_POST['password'] == 'slustice') {
// Load code below if both username
// and password submitted are correct
$_SESSION['loggedin'] = 1;
// Set session variable
header("Location: admin.php");
exit;
// Redirect to a protected page
} else echo "Wrong details";
// Otherwise, echo the error message
}
?>
<form action="?login=1" method="post" accept-charset="utf-8">
<fieldset>
<label for="username">Usermame:</label>
<input type="text" name="username" placeholder="username" required>
<label for="password">Password:</label>
<input type="password" name="password" placeholder="password" required>
<input type="submit" value="Login"> </td>
</fieldset>
</form>
这很好用。
admin.php的:
<?php
session_start();
// Call this function so your page
// can access session variables
if ($_SESSION['loggedin'] != 1) {
// If the 'loggedin' session variable
// is not equal to 1, then you must
// not let the user see the page.
// So, we'll redirect them to the
// login page (login.php).
header("Location: login.php");
exit;
}
?>
<p><a href="logout.php">Log out</a></p>
现在我的问题是,即使我点击退出网址,系统也会让我记录,如下所示:
logout.php:
<?php
session_start();
session_destroy();
header("Location: login.php");
?>
我的退出程序显然存在一些基本错误,但我似乎无法找到它...感谢您提前提供任何帮助!
答案 0 :(得分:2)
你在这里做作业:
if ($_SESSION['loggedin'] = 1) {
header("Location: admin.php");
}
你应该进行比较
if ($_SESSION['loggedin'] == 1) {
header("Location: admin.php");
}
答案 1 :(得分:0)
试试这个
<?php
session_destroy();
header('Location: index.php');
exit();
?>
答案 2 :(得分:0)
更改您的admin.php
文件
<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
header("Location: login.php");
exit;
}
?>
<p><a href="logout.php">Log out</a></p>
答案 3 :(得分:0)
在login.php中,您在验证用户详细信息后没有启动session_start ...
尝试添加session_start();在$ _SESSION [&#39; loggedin&#39;] = 1;
之前这可能对你有用......
logout.php中的在estroying之前
取消设置会话变量 使用这一行
未设置($ _ SESSION [&#39;的loggedIn&#39;]);
答案 4 :(得分:0)
为了完全终止会话,喜欢将用户注销,还必须取消设置会话ID。如果使用cookie来传播会话ID(默认行为),则必须删除会话cookie。 setcookie()可能会用于此。
使用this code(从php.net复制)安全注销:
<?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();
?>
答案 5 :(得分:0)
只需尝试以下更改:
在login.php中:
if ($_SESSION['loggedin'] == 1) {
header("Location: admin.php");
}
在logout.php中:
<?php
session_start();
ob_start();
session_destroy();
$_SESSION['loggedin']=""; //Just empty that session variable
header("Location: login.php");
?>
我认为这可以帮助您解决问题。