What are the best practices for including content in a PHP page based on URL variables?

When including content in a PHP page based on URL variables, it is important to sanitize and validate the input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. One way to achieve this is by using PHP's filter_input function to retrieve and sanitize the URL variables before using them in your code.

// Sanitize and validate the input URL variable
$page = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_STRING);

// Include content based on the sanitized URL variable
if ($page == 'about') {
    include 'about.php';
} elseif ($page == 'contact') {
    include 'contact.php';
} else {
    include 'home.php';
}