What are potential pitfalls to watch out for when including files in PHP scripts?

One potential pitfall when including files in PHP scripts is the risk of including files from untrusted sources, which can lead to security vulnerabilities such as code injection or remote file inclusion attacks. To mitigate this risk, always sanitize user input and validate file paths before including them in your script.

// Example of including files securely by validating the file path
$allowed_files = ['file1.php', 'file2.php', 'file3.php'];
$file_to_include = $_GET['file'];

if (in_array($file_to_include, $allowed_files)) {
    include($file_to_include);
} else {
    echo "Invalid file path";
}