Wenn Sie alle Elemente in $_POST
sanieren wollen, dann können Sie nur eine Hygienisierung Funktion erstellen und anwenden es alle Elemente mit array_map
:
$post_clean = array_map("sanitization_function", $_POST);
Dann würden Sie Ihre Variablen über $post_clean
statt $_POST
zugreifen.
Es wäre in etwa so aussehen:
function sanitize($dirty){
return preg_replace("/[^a-zA-Z0-9_]/", "", $dirty);
}
$cPOST = array_map("sanitize", $_POST);
if (!strlen($cPOST['username'])){
die("username is blank!");
}
Wenn Sie nur eine Teilmenge der $_POST
Elemente sanieren wollte, Sie so etwas wie tun könnte:
$cPOST = array();
$sanitize_keys = array('username','someotherkeytosanitize');
foreach($_POST as $k=>$v)
{
if(in_array($k, $sanitize_keys))
{
$cPOST[$k] = preg_replace("/[^a-zA-Z0-9_]/", "", $v);
}
else
{
$cPOST[$k] = $v;
}
}
Versuchen Sie folgendes:
$cPOST = array();
$sanitize_keys = array('username','someotherkeytosanitize');
for($_POST as $k=>$v)
{
if(in_array($k, $sanitize_keys))
{
$cPOST[$k] = preg_replace("/[^a-zA-Z0-9_]/", "", $v);
if(strlen($cPOST[$k]) == 0){
die("%s is blank", $k);
}
}
else
{
$cPOST[$k] = $v;
}
}
# At this point, the variables in $cPOST are the same as $_POST, unless you
# specified they be sanitized (by including them in the $sanitize_keys array.
# Also, if you get here, you know that the entries $cPOST that correspond
# to the keys in $sanitize_keys were not blank after sanitization.
Stellen Sie sicher, dass Sie $ sanitize_keys in ein Array von Variablen (oder $ _POST-Schlüsseln) ändern, die Sie bereinigen möchten.
Danke, also bedeutet das, dass ich immer noch die if-Anweisung auf jeden Eingang anwenden muss? – user1278496
@user: Ja du würdest. Soll das Skript "sterben", wenn eines der Elemente leer ist? – jedwards
ja Kumpel ich würde – user1278496