What are the security implications of directly outputting user input in the HTML without proper validation or sanitization, as demonstrated in the code snippet?

Directly outputting user input in HTML without validation or sanitization can lead to cross-site scripting (XSS) attacks. An attacker could inject malicious scripts into the input fields, which would then be executed by other users viewing the page, potentially leading to sensitive data theft or unauthorized actions. To prevent this, it is crucial to validate and sanitize all user input before displaying it in the HTML.

<?php
// Validate and sanitize user input
$userInput = htmlspecialchars($_POST['user_input'], ENT_QUOTES);

// Output the sanitized input in HTML
echo "<p>User input: " . $userInput . "</p>";
?>