sanitise variable php code example

Example 1: sanitize form data php

# sanitize form data
function clean($data)
{
    $data = htmlspecialchars($data);
    $data = stripslashes($data);
    $data = trim($data);
    return $data;
}

Example 2: php sanitize $POST

//If the type of each of your input variables is a string and 
//you want to sanitize them all at once, you can use:
$_GET   = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
$_POST  = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
//or 
filter_var($_POST['message'], FILTER_SANITIZE_STRING);
//or
function util_array_trim(array &$array, $filter = false)
{
    array_walk_recursive($array, function (&$value) use ($filter) {
        $value = trim($value);
        if ($filter) {
            $value = filter_var($value, FILTER_SANITIZE_STRING);
        }
    });

    return $array;
}

Tags:

Php Example