What are the potential security risks of including external files in PHP?

Including external files in PHP can pose security risks such as code injection, file inclusion vulnerabilities, and potential access to sensitive information. To mitigate these risks, it is important to sanitize user input, validate file paths, and restrict access to only necessary files.

// Example of including an external file securely
$allowed_files = ['file1.php', 'file2.php']; // List of allowed files
$file_to_include = $_GET['file'];

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