What are the potential pitfalls of including external files in PHP scripts?

Including external files in PHP scripts can introduce security vulnerabilities if the included files are not properly sanitized. This can lead to code injection attacks or unauthorized access to sensitive information. To mitigate these risks, it is important to validate and sanitize any user input before including external files in PHP scripts.

// Validate and sanitize user input before including external files
$filename = filter_input(INPUT_GET, 'file', FILTER_SANITIZE_STRING);
if ($filename !== false) {
    include($filename);
} else {
    echo "Invalid file name";
}