What are the potential issues with using the include function in PHP to load files based on URL parameters?

Using the include function in PHP to load files based on URL parameters can lead to security vulnerabilities such as directory traversal attacks. To prevent this, it is recommended to validate and sanitize the input before including the file.

// Sanitize the URL parameter before including the file
$filename = filter_var($_GET['file'], FILTER_SANITIZE_STRING);

// Validate the file path to ensure it only includes allowed files
$allowed_files = ['file1.php', 'file2.php', 'file3.php'];
if (in_array($filename, $allowed_files)) {
    include $filename;
} else {
    echo "Invalid file requested.";
}