How can developers protect against SQL injection, XSS, and RFI vulnerabilities in PHP?

To protect against SQL injection, developers should use prepared statements with parameterized queries to sanitize user input. To prevent XSS attacks, input validation and output encoding should be used to sanitize user input before displaying it on a webpage. To guard against RFI vulnerabilities, developers should avoid using user-controlled input to include files and instead use whitelisting or hardcoded file paths.

// Protecting against SQL injection
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->bindParam(':username', $username);
$stmt->execute();

// Protecting against XSS attacks
$clean_input = htmlspecialchars($_POST['input'], ENT_QUOTES, 'UTF-8');
echo $clean_input;

// Protecting against RFI vulnerabilities
$allowed_files = array('file1.php', 'file2.php');
$requested_file = $_GET['file'];
if (in_array($requested_file, $allowed_files)) {
    include($requested_file);
} else {
    echo 'Access denied';
}