How can PHP code be structured to prevent unauthorized access to files in different directories when using include statements?

To prevent unauthorized access to files in different directories when using include statements in PHP, you can use the `__DIR__` magic constant to ensure that included files are only accessed from specific directories. By checking the `__DIR__` constant against a predefined list of allowed directories, you can restrict access to files outside of those directories.

$allowed_directories = ['path/to/allowed/directory1', 'path/to/allowed/directory2'];

$included_file = 'path/to/included/file.php';

if (in_array(dirname(__FILE__), $allowed_directories)) {
    include $included_file;
} else {
    // Handle unauthorized access
    die("Unauthorized access");
}