Created
May 4, 2012 09:17
-
-
Save ctrl-freak/2593530 to your computer and use it in GitHub Desktop.
PHP User-input Sanitation and Defaults
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| // 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