What are the security implications of including files in PHP and how can they be mitigated?

Including files in PHP can pose a security risk if the included file is not properly sanitized. This can lead to code injection attacks or exposing sensitive information. To mitigate this risk, always sanitize user input before including files and avoid using user input directly in include statements.

// Sanitize user input before including files
$filename = filter_var($_GET['file'], FILTER_SANITIZE_STRING);
include 'path/to/directory/' . $filename;