What are the potential pitfalls of using include or require_once to include external files in PHP scripts?

Using include or require_once to include external files in PHP scripts can potentially lead to security vulnerabilities if the included files are not properly validated. To mitigate this risk, always ensure that the paths to included files are sanitized and validated before including them in your script.

$included_file = "path/to/external/file.php";

if (strpos($included_file, '../') === false) {
    include $included_file;
} else {
    echo "Invalid file path";
}