0% found this document useful (0 votes)
14 views1 page

HMS Auth - PHP File

This PHP script handles user authentication by checking the provided username and password against a database. It uses prepared statements to prevent SQL injection and verifies the password using password hashing. Upon successful login, it starts a session and redirects the user to the dashboard; otherwise, it redirects to the login page with an error message.

Uploaded by

Atta Gh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

HMS Auth - PHP File

This PHP script handles user authentication by checking the provided username and password against a database. It uses prepared statements to prevent SQL injection and verifies the password using password hashing. Upon successful login, it starts a session and redirects the user to the dashboard; otherwise, it redirects to the login page with an error message.

Uploaded by

Atta Gh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

<?

php
// includes/auth.php
session_start();
require_once 'db.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = trim($_POST['username']);
$password = trim($_POST['password']);

// Prevent SQL injection


$stmt = $conn->prepare("SELECT id, username, password, role FROM users WHERE
username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->store_result();

// Check if user exists


if ($stmt->num_rows == 1) {
$stmt->bind_result($id, $user, $hashed_password, $role);
$stmt->fetch();

if (password_verify($password, $hashed_password)) {
// Login success
$_SESSION['user_id'] = $id;
$_SESSION['username'] = $user;
$_SESSION['role'] = $role;
header("Location: ../dashboard.php");
exit();
} else {
header("Location: ../login.php?error=Incorrect password");
exit();
}
} else {
header("Location: ../login.php?error=User not found");
exit();
}
} else {
header("Location: ../login.php");
exit();
}

You might also like