我正在为学校的迷你项目制作登录系统。我已经完成了注册部分,但现在我必须做登录部分。我的问题是我似乎无法访问其他页面上的会话变量。它没有在另一页上返回任何内容。我很想知道我在这里做错了什么。
注意:如果他们已登录,我将其发送回注册页面。这只是一个考验。
login.php:
<?php
session_start();
if (isset($_POST['submit']))
{
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password, "vbproject");
if ($conn->connect_error) {
die("connection failed: " . $conn->connect_error);
}
$username = $conn->real_escape_string($_POST["username"]);
$password = $conn->real_escape_string($_POST["wachtwoord"]);
$query = "SELECT `userID` FROM `users` where username='$username' and password='$password'";
if ($result = $conn->query($query))
{
if($row = $result->fetch_assoc())
{
$_SESSION["ID"] = $row["userID"];
$_SESSION["name"] = $row["username"];
header("Location: ../web/register.php");
}
$result->close();
}
}
?>
register.php:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
if(isset($_SESSION['ID'])){
echo "You are logged in";
}
?>
</body>
编辑:
`<?php
session_start();
if(isset($_SESSION['name'])){
echo $_SESSION['name'];
}
?>`