Skip to content
PHP Logo

How can the script be modified to prevent XSS vulnerabilities?

To prevent XSS vulnerabilities, input validation and output encoding are essential. Input validation ensures that only expected and safe input is accepted, while output encoding ensures that any user input displayed on the page is properly escaped to prevent it from being interpreted as code.

php
<?php
// Input validation
$user_input = $_POST['user_input'];
$clean_input = htmlspecialchars($user_input, ENT_QUOTES, 'UTF-8');

// Output encoding
echo $clean_input;
?>