I'm making a simple Login system using a hashed password and session, the hashed password is being set in another page. How can I make the code recognize the hash which is set in another page?
<?php
session_start();
$servername = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "usersystem";
$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
$Username = $_POST["Username"];
$Password = $_POST["Password"];
$_SESSION['user'] = "";
$hash = password_hash($Password, PASSWORD_BCRYPT);
$sql = "SELECT * FROM users WHERE Username = '$Username'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
if (password_verify($Password, $hash)) {
$_SESSION['user'] = $_POST["Username"];
echo($_SESSION['user']);
}
else {
echo("Incorrect Username");
}
}
else {
echo("Incorrect Password");
}
$conn->close();
?>
The register page where the data is inserted into the database:
<?php
$servername = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "usersystem";
$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
$Username = $_POST["usrnm"];
$Password = $_POST["psw"];
$hash = password_hash($Password, PASSWORD_BCRYPT);
$sql = "INSERT INTO users (Username, Password) VALUES ('$Username', '$hash')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>