What are the security implications of using the include function in PHP?
Using the include function in PHP can pose security risks if the included file is not properly sanitized. This can lead to vulnerabilities such as remote code execution or file inclusion attacks. To mitigate these risks, it is important to validate and sanitize user input before including any files.
// Example of including a file with proper validation
$allowed_files = ['file1.php', 'file2.php', 'file3.php'];
$file_to_include = $_GET['file'];
if (in_array($file_to_include, $allowed_files)) {
include($file_to_include);
} else {
echo "Invalid file requested";
}