Hi there:
I inherited some code that I'm improving. The original author called a function to validate entries from a form.
The validation checks that "all" the blanks are filled, makes sure the email address is in a recognizable email addy form, checks several entries that need to be numeric to be so, picks a proper registration fee to show based on 3 cut-off dates, and checks whether the registrant has already registered once by quickly scanning the database (by a sub-function call). After the validation function returns, then the input is made to the database.
Unfortunately, this old code used Register_Globals "on".
First, a digression--what are your thoughts on the following snippet to give me local variables from the POST??
foreach($_POST as $key=>$value){
$$key = $value;
}
Now back to variable sanitization:
Then I"ve searched the entire forum for "sanitize variables" and have had interesting reading. Brad, and several of you had good feedback. I found one thread where a gent had a snippet that I thought looked efficient. There wasn't any specific critique of his snippit:
if(get_magic_quotes_gpc()) {
$username = stripslashes($_POST['username']);
$password = stripslashes($_POST['password']);
} else {
$username = $_POST['username'];
$password = $_POST['password'];
}
function CleanArray($array) {
foreach ($array as $key => $value) {
$array[$key] = mysql_real_escape_string($value);
}
return $array;
}
$_POST = CleanArray($_POST);
I'm actually not using username and password but would stripslasshes be good for name and address string entries?
Lastly, the validation function previously was fed the POST array. Now I will feed it another array after sanitization. Or, what about moving sanitizing to within the validation function as another step? Thoughts?
Any other things to consider about sanitizing user input from a form? Should I make sure I use sprintf?
THanks
Bruce Richardson