Home > Dev > PHP Security > XSS Protection

XSS Protection

This code sanitizes user input to prevent cross-site scripting (XSS) attacks.

function sanitize_input($data) {
    return htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
}

// Usage example
$user_input = '<script>alert("xss");</script>';
$safe_input = sanitize_input($user_input);
echo $safe_input;
Back