Here is the code:
<?php # Add the Post or Get fields coming in to specify filter. # Default: filter string $filters = array( 'my_text' => 'string', 'my_email' => 'email', 'my_url' => 'url', 'my_chars' => 'special', 'my_int' => 'int', 'my_float' => 'float', 'my_encoded' => 'encoded' ); foreach($_POST as $key=>$value){ if(array_key_exists($key, $filters)){ switch ($filters[$key]){ case 'string': $_POST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_STRING); $_REQUEST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_STRING); break; case 'email': $_POST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_EMAIL); $_REQUEST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_EMAIL); break; case 'url': $_POST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_URL); $_REQUEST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_URL); break; case 'special': $_POST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS); $_REQUEST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS); break; case 'int': $_POST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_INT); $_REQUEST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_INT); break; case 'float': $_POST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_FLOAT); $_REQUEST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_FLOAT); break; case 'encoded': $_POST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_ENCODED); $_REQUEST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_ENCODED); break; default : $_POST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_STRING); $_REQUEST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_STRING); } } else { $_POST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_STRING); $_REQUEST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_STRING); } } foreach($_GET as $key=>$value){ if(array_key_exists($key, $filters)){ switch ($filters[$key]){ case 'string': $_GET[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_STRING); $_REQUEST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_STRING); break; case 'email': $_GET[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_EMAIL); $_REQUEST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_EMAIL); break; case 'url': $_GET[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_URL); $_REQUEST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_URL); break; case 'special': $_GET[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS); $_REQUEST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS); break; case 'int': $_GET[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_INT); $_REQUEST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_INT); break; case 'float': $_GET[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_FLOAT); $_REQUEST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_FLOAT); break; case 'encoded': $_GET[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_ENCODED); $_REQUEST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_ENCODED); break; default : $_GET[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_STRING); $_REQUEST[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_STRING); } } else { $_GET[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_STRING); $_REQUEST[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_STRING); } } ?>
REFERENCES
http://scovol.net/2010/02/12/generic-input-sanitizer/