How can PHP be used to handle user authentication and password protection in a scheduling application?
To handle user authentication and password protection in a scheduling application using PHP, you can create a login system where users input their username and password. The passwords should be securely hashed before being stored in a database. When a user tries to log in, their entered password should be hashed and compared to the hashed password stored in the database.
<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve user input
$username = $_POST["username"];
$password = $_POST["password"];
// Hash the password
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
// Check if the username and password match a record in the database
// This is a simplified example, in a real application, you would query your database
if ($username == "admin" && password_verify($password, $hashedPassword)) {
// Authentication successful, redirect to the scheduling application
header("Location: scheduling_app.php");
exit();
} else {
// Authentication failed, display an error message
echo "Invalid username or password";
}
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="username" placeholder="Username" required><br>
<input type="password" name="password" placeholder="Password" required><br>
<button type="submit">Login</button>
</form>
Related Questions
- What are some common pitfalls to avoid when trying to display shopping cart results on a separate page using PHP?
- How can error handling be improved in the provided PHP code for inserting form data into a database?
- What could be causing a PHP script to only function when directly accessed, but not when called from another HTML page?