How can the login_check function be improved to enhance the security and reliability of user authentication?
Issue: The login_check function currently only checks if the username and password match a hardcoded value, which is not secure. To enhance security and reliability, the function should query a database to validate the user credentials.
// Improved login_check function to query a database for user authentication
function login_check($username, $password) {
// Connect to the database
$conn = new mysqli('localhost', 'username', 'password', 'database_name');
// Query the database for the user with the provided username and password
$query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$result = $conn->query($query);
// Check if a matching user was found
if ($result->num_rows == 1) {
return true; // User authenticated successfully
} else {
return false; // User authentication failed
}
// Close the database connection
$conn->close();
}