Created
August 17, 2013 01:47
-
-
Save bencentra/6254856 to your computer and use it in GitHub Desktop.
A basic whitelist to prevent XSS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function xss_whitelist($input, $limit = null, $offset = 0) | |
{ | |
// Force input to be a string0 | |
$x = (string) $input; | |
// Allow alphanumeric characters, whitespace, and specific characters | |
$x = preg_replace("/[^a-zA-Z0-9 -:,.!?\/|]/", "",$x); | |
// Limit characters | |
if ($limit) { | |
$x = substr($x, $offset, $limit); | |
} | |
// Convert characters to HTML entities and return the sanitized string | |
return htmlentities($x, ENT_QUOTES, 'UTF-8'); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment