Skip to content

Instantly share code, notes, and snippets.

@nawawi
Last active September 11, 2015 19:18
Show Gist options
  • Save nawawi/20b6d5a3703be181d601 to your computer and use it in GitHub Desktop.
Save nawawi/20b6d5a3703be181d601 to your computer and use it in GitHub Desktop.
simple xss protection
<?php
function _array_map_recursive($func, $arr) {
$new = array();
foreach($arr as $key => $value) {
$new[$key] = (is_array($value) ? _array_map_recursive($func, $value) : ( is_array($func) ? call_user_func_array($func, $value) : $func($value) ) );
}
return $new;
}
function _escape_html($string) {
// 4th parameter for double_encode set to false, ignore if already encoded
return htmlspecialchars($string, ENT_QUOTES, 'UTF-8', false);
}
function _escape_xss($data) {
if ( is_array($data) ) {
return _array_map_recursive('_escape_html',$data);
}
return ( !is_null($data) ? _escape_html($data) : null );
}
if ( !empty($_GET) ) $_GET = _array_map_recursive('_escape_html', $_GET);
if ( !empty($_POST) ) $_POST = _array_map_recursive('_escape_html', $_POST);
if ( !empty($_REQUEST) ) $_REQUEST = _array_map_recursive('_escape_html', $_REQUEST);
if ( !empty($_COOKIE) ) $_COOKIE = _array_map_recursive('_escape_html', $_COOKIE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment