Я пытаюсь создать небольшую страницу входа на PHP с помощью куки. У меня есть база данных / таблица в phpMyAdmin, которая связана с ним. Все выглядит хорошо, но когда я сам пробую страницу и ввожу имя пользователя / пароль, я в конечном итоге оказываюсь на странице администратора (в противном случае я получаю сообщение "упс, похоже, вы ошиблись"), но с сообщением об ошибке, сообщающим мнеЯ не авторизован. В чем проблема? Чувствуется, что он прямо передо мной, но не видит его.
Мой код со страницами индекса, администратора и базы данных:
<?php
//log in
if (isset($_POST["log"]) && $_POST["log"] == "1") {
$_POST["log"] = 0;
$identifiant = htmlentities ($_POST["username"]);
$password = htmlentities ($_POST["passwd"]);
$encryp_pwd = hash("sha256", $password);
require_once("database.php");
$sql = "SELECT * FROM MyTable WHERE User='$identifiant' AND Passwd='$encryp_pwd'; ";
$result = $connexion->query($sql) or die ("Oops, something went wrong");
if(mysqli_num_rows ($result) == 1){
//set cookie and a time
setcookie('logged_in', $identifiant, time()+60*60*24);
header("location: admin.php");
}
else
echo "<h2>Seems like you got it wrong</h2> <a href='index.php'>try again</a>";
}
else {
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Welcome</h1>
<form action="index.php" method="post">
<input type="text" name="username" placeholder="identifiant" /> <br/>
<input type="text" name="passwd" placeholder="Password" /> <br/>
<input type="submit" value="log in" />
<input type="hidden" name="log" value="1" />
</form>
</body>
</html>
<?php
}
?>
admin ниже
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Admin</h1>
<?php
//check if there is a cookie and if the person is logged in
if (isset($_COOKIE['logged_in'])) {
echo "<h2>You are logged in as: " . $_COOKIE['logged_in'] . "</h2>";
echo "<a href='logout.php'>Click here to log out</a>";
}
//Message + link to the log in page if not
else {
echo "<p>You are not logged in</p>";
echo "<a href='index.php'>Click here to log in</a>";
}
?>
</body>
</html>
база данных ниже
<?php
$serveur = "localhost";
$user = "xxxx";
$motdepass = "xxxx";
$base = "xxxx";
$connexion = new mysqli($serveur, $user, $motdepass, $base);
?>