I’m fairly new to PHP even though I’ve been coding for 25+ years. The issue I’m having is my login script. I know it seems pretty easy and straight-forward but I’m hung up on the password_verify() function. I’ve hashed my password in the database table. Below is the code that I copied from “Chatgpt.com” with changes reflecting my varialbe/table names.
Thanks,
Blake
<?php
require "dbconnection_pdo.php";
// Start session
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = htmlspecialchars($_POST['username'] ?? '');
$password = htmlspecialchars($_POST['password'] ?? '');
// Query to find the user
$sql = "SELECT password, user_handle FROM food.users WHERE username = :username";
$stmt = $pdo->prepare($sql);
$stmt->execute(['username' => $username]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
echo $password . "<br>";
echo $user['password'];
if ($user && password_verify($password, $user['password'])) {
$SESSION['handle'] = $user['user_handle'];
header('Location: index.php');
exit();
} else {
echo "<script>document.getElementById('msg').innerHTML = 'Invalid login credentials!';</script>";
}
}