Last active
September 11, 2015 19:18
-
-
Save nawawi/20b6d5a3703be181d601 to your computer and use it in GitHub Desktop.
simple xss protection
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 _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