所以,我有:
问题是,即使在我正确登录之前,我也可以随时访问home.php。 我将start_session()放在需要$ _SESSION变量的每个页面上,并将session_destroy()放在logout.php中。
所以这里是php文件'代码:
的index.php
<body>
<?php
require_once('config.php');
if ($maintanance) {
echo "Az oldal karbantartás alatt van.";
}
else if ($db_conn_error) {
echo "Something went wrong according to database connection.";
}
else {
include('reg.php');
include('./templates/header.php');
?>
<section>
<form id="login_form" action="" method="POST">
<h2>Already a member? Sign in!</h2>
<p>Username: <input type="text" name="username"></p>
<p>Password: <input type="password" name="password"></p>
<input type="submit" name="login_submit" value="Sign In">
<?php include 'login.php'; ?>
</form>
<form id="reg_form" action="" method="POST" onsubmit="return validation();">
<h2>Sign up Now!</h2>
<p>Username: <input type="text" name="username" placeholder="min. 5 characters">
<span id="user_error"></span>
</p>
<p>Password: <input type="password" name="password" placeholder="min. 8 characters"></p>
<p>Password again: <input type="password" name="password_again"></p>
<p>E-mail: <input type="email" name="email" size="30"></p>
<p>Date of birthday:
<input type="number" name="bd_year" min="1950" max="2016">
<input type="number" name="bd_month" min="1" max="12">
<input type="number" name="bd_day" min="1" max="31">
</p>
<input type="submit" name="reg_submit" value="Sign Up">
</form>
</section>
</body>
</html>
<?php } ?>
的login.php
<?php
include 'config.php';
if (isset($_POST["login_submit"]))
{
$username = $_POST["username"];
$password = $_POST["password"];
$query = "SELECT username, hashed_password FROM users WHERE username = '$username';";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
$rows_num = mysqli_num_rows($result);
$password_match_error_message = false;
if ($rows_num == 0) {
echo "<p class='login_error_msg'>This user doesn't exist!</p>";
}
else {
$password_match = password_verify($password, $row['hashed_password']);
if (!$password_match) {
echo "<p class='login_error_msg'>Wrong password!</p>";
}
else {
session_start();
$_SESSION["user"] = $username;
header("Location: home.php");
}
}
}
?>
home.php
<?php
session_start();
if (isset($_SESSION["user"])) {
?>
<!DOCTYPE html>
<html>
<head>
<title>Spookie - Social Network</title>
<link rel="stylesheet" type="text/css" href="./css/style.css">
</head>
<body>
<?php
include './templates/header.php';
?>
<?php } else { echo "You are not logged in!"; } ?>
</body>
</html>
logout.php
<?php
session_unset($_SESSION["user"]);
session_destroy();
header("Location: index.php");
?>
我知道,很难看到代码中真正发生了什么,登录工作正常,但会议并非如此。
问题:我输入并且home.php总是可以访问的,尽管我没有登录.logout.php不会破坏会话甚至会话也不会启动。
非常感谢你的帮助! :)
答案 0 :(得分:1)
问题出在logout.php。
您还应声明session_start()
以确保 CAN 删除$_SESSION["user"]
变量。
可能还有其他问题,因为我看不到整个代码。如果我错了,请纠正我。
看一下解释设置会话变量的典型方法的另一个答案
答案 1 :(得分:0)
根据本手册:http://php.net/manual/en/function.session-destroy.php
为了完全杀死会话,喜欢将用户注销掉, 会话ID也必须取消设置。如果使用cookie来传播 会话ID(默认行为),然后会话cookie必须是 删除。 setcookie()可以用于此。
手动链接有一个完整的工作示例,说明如何执行此操作。从那里被盗:
<?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 :(得分:0)
session_start()
将开始会话。
session_destroy()
会破坏会话。
要设置会话数据,您可以执行此操作。
`
$_SESSION['is_logged_in'] = true;
`
检查会话是否存在或检查用户是否已登录
`
If(isset($_SESSION['is_logged_in'] ) {}
else {
//redirect to login page
}
`