Skip to content

Instantly share code, notes, and snippets.

@ctrl-freak
Created May 4, 2012 09:17
Show Gist options
  • Select an option

  • Save ctrl-freak/2593530 to your computer and use it in GitHub Desktop.

Select an option

Save ctrl-freak/2593530 to your computer and use it in GitHub Desktop.
PHP User-input Sanitation and Defaults
<?
// Usage: search($_GET);
function search($args) {
$vars = array(
'order_by' => 'date',
'order' => 'DESC',
)
$where = array(
'tags',
'country_tags',
'text'
);
$vars = values($vars, $args);
$where = values($where, $args);
// Construct and execute DB query
}
function values($defaults, $values) {
$array = array();
if (is_assoc($defaults)) {
foreach ($defaults as $k => $v) {
if (isset($values[$k]) && $values[$k] != '') {
$array[$k] = stripslashes($values[$k]);
} else {
if ($defaults[$k] != '') {
$array[$k] = $defaults[$k];
}
}
}
} else {
foreach ($defaults as $v) {
if (isset($values[$v]) && $values[$v] != '') {
$array[$v] = stripslashes($values[$v]);
}
}
}
return $array;
}
// function is_assoc($array) {
// return (bool) count(array_filter(array_keys($array), 'is_string'));
// }
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment