I made a login and register system, it works, it encrypts the password using bcrypt, but when I go to login it only works with the encrypted password so I have to go to the database and copy it.
This is my login and to help next is Register. I tried to put the password_hash in the login too but when doing that I can’t login.
<label for="password">Password:</label>
<input type="password" id="password" name="password" ><br><br>
<input type="submit" name="login" value="Login">
if (isset($_POST['login'])) {
$username = $_POST['username'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$stmt = $conn->prepare("SELECT * FROM users WHERE username=? AND password=?");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$result = $stmt->get_result();
if (mysqli_num_rows($result) == 1) {
echo "Login successful";
header("Location: forminicio.php");
} else {
echo "Invalid username or password";
}
}
Register
if (isset($_POST['register'])) {
$data['username'] = ($_POST['username']);
$data['password'] = password_hash($_POST['password'], PASSWORD_DEFAULT);
$username = $data['username'];
$password = $data['password'];
$sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
if (mysqli_query($conn, $sql)) {
echo "New user created successfully";
} else {
echo "Error creating user: " . mysqli_error($conn);
}
}