Skip to content
PHP Logo

What are the potential pitfalls of directly outputting data from a textarea in PHP without sanitizing it?

Directly outputting data from a textarea in PHP without sanitizing it can leave your application vulnerable to Cross-Site Scripting (XSS) attacks. To prevent this, you should always sanitize user input before displaying it on your website. This can be done by using functions like htmlspecialchars() to escape special characters and prevent them from being interpreted as HTML or JavaScript.

php
// Sanitize user input from textarea before outputting
$text = $_POST['textarea'];
$sanitized_text = htmlspecialchars($text);

echo $sanitized_text;