What are common methods for including external files in PHP, and what precautions should be taken to prevent security vulnerabilities?

When including external files in PHP, common methods include using include(), require(), include_once(), or require_once(). To prevent security vulnerabilities, it is important to validate user input, sanitize data, and avoid using user input directly in file paths to prevent directory traversal attacks.

// Example of including an external file with proper validation and sanitization
$filename = 'path/to/file.php';
if (file_exists($filename)) {
    include($filename);
} else {
    echo 'File not found';
}