What are the potential security risks of including or requiring scripts within other scripts in PHP?

Including or requiring scripts within other scripts in PHP can pose security risks such as code injection, file path traversal, and unauthorized access to sensitive information. To mitigate these risks, it is important to sanitize input data, validate file paths, and restrict access to critical files.

// Example of including a script with proper validation
$allowed_scripts = ['script1.php', 'script2.php'];
$requested_script = $_GET['script'];

if (in_array($requested_script, $allowed_scripts)) {
    include $requested_script;
} else {
    echo "Unauthorized access";
}