What are the steps involved in creating a custom login form for directory protection in PHP?
Directory protection in PHP can be achieved by creating a custom login form that prompts users to enter a username and password before accessing the protected directory. This can be done by using PHP sessions to store login credentials and checking them against a predefined set of username and password. Below is a sample code snippet that demonstrates how to create a custom login form for directory protection in PHP:
<?php
session_start();
$valid_username = "admin";
$valid_password = "password";
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
if($username == $valid_username && $password == $valid_password){
$_SESSION['logged_in'] = true;
header("Location: protected_directory.php");
exit();
} else {
echo "Invalid username or password";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
</head>
<body>
<h2>Login Form</h2>
<form method="post" action="">
<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" name="submit" value="Login">
</form>
</body>
</html>