How can PHP be effectively used to create a secure login system for admins on a website?
To create a secure login system for admins on a website using PHP, you can use techniques such as hashing passwords, using prepared statements to prevent SQL injection, and implementing session management to track user authentication.
<?php
session_start();
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Check if the admin credentials are valid
if ($_POST["username"] == "admin" && password_verify($_POST["password"], '$2y$10$RkQBA1Uf5O1qT7N8Vwvz8uYzWV1G7U8pDfO6iQJmUj3ZGhUzS3xO2')) {
$_SESSION["admin"] = true;
header("Location: admin_dashboard.php");
exit();
} else {
echo "Invalid username or password";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Admin Login</title>
</head>
<body>
<h2>Admin Login</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="username">Username:</label><br>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>