What is the recommended method for updating configuration settings in a PHP file using a form?

When updating configuration settings in a PHP file using a form, it is recommended to first validate the input data to ensure it is safe and properly formatted. Once validated, you can update the configuration settings in the PHP file by reading the form data and writing it back to the file. It is important to handle errors gracefully and provide feedback to the user after the update is complete.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $configFile = 'config.php';

    $newSetting = $_POST['new_setting'];

    // Validate new setting here

    $configContent = file_get_contents($configFile);
    $configContent = preg_replace('/\$setting = \'.*\'/', "\$setting = '$newSetting'", $configContent);
    
    file_put_contents($configFile, $configContent);

    echo "Configuration settings updated successfully!";
}
?>