Home › Forums › Back End › How to not submit form with random spaces or without text? › Reply To: How to not submit form with random spaces or without text?
May 5, 2014 at 10:53 am
#169244
shaneisme
Participant
Yeah, PHP empty will return false with a string that is spaces only.
I normally have a method that scrubs a few basics on user inputs to stop things like XSS or blank fields on any POST
or GET
.
This is not very “performant”, or very clean, but maybe it’ll give you an idea:
function cleanup($n) {
if (is_array($n)) {
return array_map('cleanup', $n);
} else {
$n = trim($n);
$n = htmlspecialchars($n);
return $n;
}
}
if (!empty($_POST)) { // This will call cleanup for all user inputs
$_POST = array_map('cleanup', $_POST);
}
This way, every time a users POST
‘s a form the data will get scrubbed before it reaches your checks (assuming you do this immediately in order of operation).