所有非程式碼直接寫入的資料(GET、POST、資料庫、Cookie、Session)均視為「有瑕疵」。
$myUsername = $_POST['username']; // 受污染
$arrayUsers = array($myUsername, 'tom', 'tommy'); // 陣列也受污染
define("GREETING", 'hello ' . $myUsername); // 常數也受污染
function cleanInput($input) {
$clean = strtolower($input);
$clean = preg_replace("/[^a-z]/", "", $clean); // 僅保留小寫字母
$clean = substr($clean, 0, 12); // 限制長度
return $clean;
}
$myUsername = cleanInput($_POST['username']); // 乾淨
$arrayUsers = array($myUsername, 'tom', 'tommy'); // 乾淨
define("GREETING", 'hello ' . $myUsername); // 乾淨