What potential issues can arise when including multiple files based on user input in PHP?

One potential issue that can arise when including multiple files based on user input in PHP is the risk of directory traversal attacks, where an attacker can manipulate the input to access sensitive files outside the intended directory. To mitigate this risk, it is crucial to sanitize and validate user input before including any files. Implementing a whitelist approach by only allowing specific files to be included based on predefined criteria can help prevent such attacks.

// Sanitize and validate user input before including files
$user_input = $_GET['file'];

// Define an array of allowed files
$allowed_files = ['file1.php', 'file2.php'];

// Check if user input is in the list of allowed files
if (in_array($user_input, $allowed_files)) {
    include $user_input;
} else {
    // Handle invalid input
    echo "Invalid file input";
}