Skip to content

Instantly share code, notes, and snippets.

@jovialcore
Last active December 14, 2022 09:53
Show Gist options
  • Save jovialcore/4d4ef29217e9fb5d243425aed930998b to your computer and use it in GitHub Desktop.
Save jovialcore/4d4ef29217e9fb5d243425aed930998b to your computer and use it in GitHub Desktop.
<?php
//Using php, Native approaches involve like htmlspecialchars() to escape special characters in the user input.
$Userdata;
$sanitized = htmlspecialchars($Userdata);
// output
echo " <p> Your data is:" . $sanitized . " </p>";
//CSP (Content security policy) function, By using CSP Header with PHP
header("Content-Security-Policy: default-src 'self'");
/** HTML Purifier library Approach **/
//require the HTML Purifier library
require_once '/path/to/htmlpurifier/library/HTMLPurifier.auto.php';
// instantiate
$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
// get user inputed data
$user_data = "<p>This is some <strong>dirty</strong> HTML.</p>";
// clean HTML input using HTML Purifier
$purified = $purifier->purify($user_data);
// print cleaned HTML
echo $purified;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment