What are some best practices for building a login form in PHP and storing login data in an external file?

When building a login form in PHP, it is important to securely store login data in an external file to prevent unauthorized access. One common practice is to store login information in a separate file outside of the web root directory, using encryption techniques to secure sensitive data. This helps to protect user credentials from being exposed in case of a security breach.

<?php
// External file to store login data (e.g., login_credentials.php)
$login_data = [
    'username' => 'admin',
    'password' => 'hashed_password_here' // Use password_hash() function to hash passwords
];

// Login form processing
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];

    // Verify login credentials
    if ($username == $login_data['username'] && password_verify($password, $login_data['password'])) {
        // Successful login
        echo 'Login successful!';
    } else {
        // Invalid credentials
        echo 'Invalid username or password';
    }
}
?>