How can PHP developers prevent unauthorized script inclusion and protect against external attacks on their websites?

To prevent unauthorized script inclusion and protect against external attacks, PHP developers can use the `basename()` function to extract the base name of the included file and compare it against a list of allowed files. Additionally, developers can use the `realpath()` function to resolve the actual path of the included file and ensure it is within the expected directory.

$allowed_files = ['file1.php', 'file2.php', 'file3.php'];
$included_file = basename($_GET['file']);

if (in_array($included_file, $allowed_files)) {
    $real_path = realpath('path/to/includes/' . $included_file);
    
    if (strpos($real_path, 'path/to/includes/') === 0) {
        include $real_path;
    } else {
        die('Unauthorized access');
    }
} else {
    die('Invalid file');
}