What are the potential pitfalls of using the include or require PHP commands for external page inclusion?

One potential pitfall of using the include or require PHP commands for external page inclusion is the risk of including files from untrusted sources, which could lead to security vulnerabilities such as code injection or file inclusion attacks. To mitigate this risk, always validate and sanitize user input before using it in include or require statements.

// Example of validating and sanitizing user input before including a file
$file = $_GET['file'];

// Check if $file is a valid file path
if (strpos($file, '../') === false && file_exists($file)) {
    include $file;
} else {
    echo 'Invalid file path';
}