How can a whitelist be implemented to ensure safe usage of variables in the include function in PHP?

To ensure safe usage of variables in the include function in PHP, a whitelist can be implemented to restrict the included files to a predefined list of allowed files. This helps prevent arbitrary file inclusion vulnerabilities and ensures that only trusted files are included.

$allowed_files = array("file1.php", "file2.php", "file3.php");

$included_file = $_GET['file'];

if (in_array($included_file, $allowed_files)) {
    include($included_file);
} else {
    echo "Access denied.";
}