How should HTMLPurifier be used in PHP for input and output?
When dealing with user input in PHP, it is important to sanitize the data to prevent any potential security vulnerabilities such as cross-site scripting (XSS) attacks. HTMLPurifier is a popular library that can be used to clean and filter HTML input to ensure it is safe for output on a webpage. To use HTMLPurifier in PHP, you can instantiate the HTMLPurifier object with the desired configuration settings, then pass the user input through the purify() method to sanitize it before displaying it on the webpage.
// Include HTMLPurifier library
require_once 'path/to/HTMLPurifier.auto.php';
// Create HTMLPurifier configuration
$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
// Sanitize user input
$dirty_html = '<script>alert("XSS attack!")</script>';
$clean_html = $purifier->purify($dirty_html);
// Output sanitized HTML
echo $clean_html;