What are some recommendations for securely including external files in PHP to prevent security vulnerabilities like SQL injections?

When including external files in PHP, it is important to sanitize the input to prevent security vulnerabilities like SQL injections. One way to securely include external files is to use PHP's realpath() function to get the absolute path of the file and then check if the file exists before including it.

$filename = realpath($_GET['file']);

if ($filename && file_exists($filename)) {
    include $filename;
} else {
    // Handle error or display a message
}