Can PHP developers implement a centralized script for calling different pages based on user input while maintaining security and preventing unauthorized access?

To implement a centralized script for calling different pages based on user input while maintaining security and preventing unauthorized access, PHP developers can create a router script that validates user input and only allows access to authorized pages. This router script can check the user input against a whitelist of allowed pages and redirect users accordingly.

<?php

// Define a whitelist of allowed pages
$allowedPages = array('page1', 'page2', 'page3');

// Get user input for the requested page
$userInput = $_GET['page'];

// Check if the requested page is in the whitelist
if (in_array($userInput, $allowedPages)) {
    // Include the requested page
    include($userInput . '.php');
} else {
    // Redirect to an error page or homepage
    header('Location: error.php');
    exit;
}

?>