-
-
Save wilmoore/2140151 to your computer and use it in GitHub Desktop.
Retrieve whitelisted set of parameters
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
<?php | |
function filter_params(array $params = [], array $whitelist = []) { | |
return array_map('trim', array_intersect_key($params, array_flip($whitelist))); | |
} | |
$params = ['is_admin' => 1, 'name' => ' Bob', 'surname' => 'Smith ']; | |
$whitelist = ['name', 'surname']; | |
// these are your whitelisted parameters (please don't forget to filter/validate before persisting) | |
var_dump(filter_params($params, $whitelist)); |
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
<?php | |
function filter_params(array $params = array(), array $whitelist = array()) { | |
return array_map('trim', array_intersect_key($params, array_flip($whitelist))); | |
} | |
$params = array('is_admin' => 1, 'name' => ' Bob', 'surname' => 'Smith '); | |
$whitelist = array('name', 'surname'); | |
// these are your whitelisted parameters (please don't forget to filter/validate before persisting) | |
var_dump(filter_params($params, $whitelist)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
RE: http://rubysource.com/rails-mass-assignment-issue-a-php-perspective