What are the best practices for including files in PHP scripts to prevent security vulnerabilities?

When including files in PHP scripts, it is important to follow best practices to prevent security vulnerabilities such as directory traversal attacks or code injection. One way to mitigate these risks is to use absolute file paths instead of relative paths when including files. Additionally, it is recommended to sanitize user input and validate file paths before including them in the script.

// Example of including files in PHP scripts using absolute file paths and validating input
$includedFile = '/path/to/secure/file.php';

if (strpos($includedFile, '/path/to/secure/') !== false) {
    include $includedFile;
} else {
    die('Invalid file path');
}