Skip to content
PHP Logo

What are potential security risks associated with allowing HTML code to be stored and displayed from a database using PHP?

Allowing HTML code to be stored and displayed from a database using PHP can pose security risks such as cross-site scripting (XSS) attacks. To mitigate this risk, it is important to properly sanitize and validate the HTML content before displaying it on the web page. This can be done by using functions like htmlspecialchars() to escape special characters and prevent malicious scripts from being executed.

php
// Retrieve HTML content from the database
$html_content = "<p>This is some <script>alert('malicious script');</script> HTML content</p>";

// Sanitize and display the HTML content
echo htmlspecialchars($html_content);