How can PHP developers avoid the use of $_GET variables for includes to prevent vulnerabilities?

Using $_GET variables directly in includes can lead to security vulnerabilities such as remote code execution. To prevent this, PHP developers should validate and sanitize user input before using it in includes. One way to do this is to use a whitelist approach where only specific files are allowed to be included based on predefined rules.

// Example of using a whitelist approach to prevent vulnerabilities
$allowed_files = ['header.php', 'footer.php', 'sidebar.php'];

if (isset($_GET['page']) && in_array($_GET['page'], $allowed_files)) {
    include $_GET['page'];
} else {
    // Handle error or default behavior
}