Skip to content

Instantly share code, notes, and snippets.

@birarda
Created May 2, 2012 18:03
Show Gist options
  • Save birarda/2578772 to your computer and use it in GitHub Desktop.
Save birarda/2578772 to your computer and use it in GitHub Desktop.
verifyRequestParameter
function verifyRequestParameter($param, $min = null, $max = null, $send_resp = true) {
// make sure the parameter was passed
if (!array_key_exists($param, $_REQUEST)) {
$error_message = "Missing {$param} parameter";
} else {
// if we have a max or min value make sure the parameter is in range
if (isset($min)) {
if ($_REQUEST[$param] < $min) {
$error_message = "Parameter {$param} not greater than accepted minimum value.";
}
}
if (isset($max)) {
if ($_REQUEST[$param] > $max) {
$error_message = "Parameter {$param} not less than accepted maximum value.";
}
}
}
// if we have an error message then send that back
// and return false
if (isset($error_message)) {
if ($send_resp) {
// only send the json response if the calling function asked us to
response(json_encode(array(
'error' => true,
'message' => $error_message
)));
}
return false;
} else {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment