What are some best practices for securely transmitting passwords from a form to a PHP page?

When transmitting passwords from a form to a PHP page, it is crucial to ensure the data is encrypted to prevent interception by malicious actors. One common best practice is to use HTTPS to encrypt the data in transit. Additionally, you can use PHP's password_hash() function to securely hash the password before storing it in a database.

// Ensure the form is submitted over HTTPS
if ($_SERVER['HTTPS'] !== 'on') {
    header("Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
    exit();
}

// Hash the password before storing it in the database
$password = $_POST['password'];
$hashed_password = password_hash($password, PASSWORD_DEFAULT);

// Store the hashed password in the database
// Example: $query = "INSERT INTO users (username, password) VALUES ('$username', '$hashed_password')";