How can the use of whitelisting techniques improve the security of file inclusion in PHP scripts?

Using whitelisting techniques can improve the security of file inclusion in PHP scripts by only allowing specific, known files to be included, preventing attackers from including malicious files. This helps to mitigate the risk of remote code execution and other security vulnerabilities associated with file inclusion.

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

if (in_array($_GET['file'], $allowed_files)) {
    include($_GET['file']);
} else {
    echo "Invalid file included.";
}