What best practices should be followed when creating a "Password change field" in a PHP login script without MySQL?

When creating a "Password change field" in a PHP login script without MySQL, it is important to ensure that the user is authenticated before allowing them to change their password. This can be done by verifying the user's current password before allowing them to input a new one. Additionally, it is crucial to securely hash the new password before storing it in a file or another data storage method.

// Check if the user is authenticated before allowing them to change their password
if(isset($_SESSION['user_id'])){
    // Verify the user's current password before allowing them to change it
    $current_password = $_POST['current_password'];
    $new_password = $_POST['new_password'];
    
    // Securely hash the new password before storing it
    $hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
    
    // Update the user's password in the data storage method
    // For example, saving it to a file
    file_put_contents('passwords.txt', $_SESSION['user_id'] . ':' . $hashed_password . PHP_EOL, FILE_APPEND);
    
    echo "Password changed successfully!";
} else {
    echo "You are not authenticated to change your password.";
}