What are potential security risks associated with using $_GET variables in PHP for dynamic content inclusion?

Using $_GET variables directly in PHP for dynamic content inclusion can expose your application to security risks such as SQL injection, cross-site scripting (XSS), and directory traversal attacks. To mitigate these risks, it is important to sanitize and validate the input before using it in your code.

// Sanitize and validate the $_GET variable before using it
$page = isset($_GET['page']) ? $_GET['page'] : 'default';
$allowed_pages = ['about', 'contact', 'services'];

if (in_array($page, $allowed_pages)) {
    include 'pages/' . $page . '.php';
} else {
    include 'pages/default.php';
}