RealImmortal2004
Coder
I'm trying to create a working login system. When I register, the data is submitting correctly but when I go to login, it won't perform the functions that it is supposed to. It's supposed to go to the admin.php page only if the credentials are valid but it won't execute the function. Where am I going wrong in my code below?
PHP:
<?php
// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=accounts', 'root', '');
// Retrieve form data
$user_id = $_POST['user_id'];
$password = $_POST['password'];
// Check if the user exists in the database
$stmt = $pdo->prepare("SELECT * FROM users WHERE user_id = ?");
$stmt->execute([$user_id]);
$user_id = $stmt->fetch();
if ($user_id && password_verify($password, $user_id['password'])) {
// User authenticated, store user session or redirect to a protected page
session_start();
$_SESSION['user_id'] = $user_id['user_id'];
header("Location: admin.php");
exit();
} else {
header("Location: index.php");
die();
}
?>