I always use filter_var($var, FILTER, FLAG); when I get data from $_GET, $_POST and so on, but now this data is a JSON string but I didn't find any filter to sanitize JSON. Anyone know how to implement this filter?
PHP filter_var(): http://php.net/manual/en/function.filter-var.php
PHP FILTER CONST: http://php.net/manual/en/filter.filters.sanitize.php
Parse the JSON first into a PHP array and then filter each value in the array as you do with regular request content, you could map the JSON keys to schematic filters and flags/options e.g.
$filters = array(
'email'=>FILTER_VALIDATE_EMAIL,
'url'=>FILTER_VALIDATE_URL,
'name'=>FILTER_SANITIZE_STRING,
'address'=>FILTER_SANITIZE_STRING
);
$options = array(
'email'=>array(
'flags'=>FILTER_NULL_ON_FAILURE
),
'url'=>array(
'flags'=>FILTER_NULL_ON_FAILURE
),
//... and so on
);
$inputs = json_decode($your_json_data);
$filtered = array();
foreach($inputs as $key=>$value) {
$filtered[$key] = filter_var($value, $filters[$key], $options[$key]);
}
You use filter_var_array for this:
$inputs = filter_var_array( json_decode( $your_json_data, true ), [
'email' => [ 'filter' => FILTER_VALIDATE_EMAIL,
'flags' => FILTER_NULL_ON_FAILURE ],
'url' => [ 'filter' => FILTER_VALIDATE_URL,
'flags' => FILTER_NULL_ON_FAILURE ],
'name' => FILTER_VALIDATE_NAME,
'address' => FILTER_SANITIZE_STRING
] );
Related
php array:
$post = [
'contact' => [ // <--
['phone' => '1234', 'lname'=>'Anton'],
['phone' => '123', 'lname'=>'Sima']
] // <--
];
or GET URL:
contact[][phone]=1234&contact[][lname]=Anton
How can I get data in this format using Django request ?
I used https://github.com/bernii/querystring-parser, its result does not suit me, it deletes duplicate keys
PHP can't use duplicate keys, you need to add another array around them, if you want something like your example:
$post = [
'contact' => [ // <--
['phone' => '1234', 'lname'=>'Anton'],
['phone' => '123', 'lname'=>'Sima']
] // <--
];
I have the following array
$array['projects'] = [
'name1' => [
'task' => [
'tags' => ['value1', 'value2'],
'email' => 'email2',
'description' => 'mpla'
],
'email' => 'email1',
'tags' => ['value1', 'value3'],
'title' => 'mpla'
]
];
Is there anyway I could use the Hash class of CakePHP 3 or maybe another class of CakePHP framework to achieve the following result:
$array['projects'] = [
'name1' => [
'email' => 'email2',
'tags' => ['value1', 'value2'],
'title' => 'mpla'
'desciption' => 'mpla'
]
];
If you also know anyother package that it can handle arrays and get my job done it will do.
Not sure that this can be easily achieved using Cake's Hash utility. You can easily extract the array items indexed by task using combine(), but not sure how you would then go about extracting the title values and combining those with the other array elements using Hash:-
Hash::combine($array, 'projects.{s}', 'projects.{s}.task');
Perhaps the simplest solution is to use a foreach loop like this:-
$data = [];
foreach ($array['projects'] as $value) {
$data['projects'] = $value['task'] + ['title' => $value['title']];
}
I'm trying to validate this input:
$values = [
'id' => $input['id'][$i],
'template_id' => $input['template_id'][$i],
'schedulable_id' => $id,
'schedulable_type' => $type,
'order_by' => $i
];
Against these rules found in my Schedule class:
public static $rules = [
'template_id' => 'required|integer|exists:templates,id',
'schedulable_id' => 'required|integer',
'schedulable_type' => 'required|in:Item,Order',
'order_by' => 'integer'
];
When I do the following, I always get an array to string conversion error in "/laravel/vendor/laravel/framework/src/Illuminate/Validation/Validator.php" on line 905:
$validator = Validator::make($values, Schedule::$rules);
if ($validator->fails()) {
$errors[$i] = $validator->messages();
continue;
}
Why would this be happening?
Just discovered I had Ardent's $forceEntityHydrationFromInput = true and my input cannot be pulled directly from Input for validation purposes due to the fact that it is submitted as an array of partially referenced values.
To fix this, change to $forceEntityHydrationFromInput = false and use standard input validation procedure instead of relying on Ardent's magic.
Sometimes clever packages are too clever.
I'm not sure if the title of this question is necessarily the accurate description of what I need to do, but I'll go ahead and ask my question and see what everyone thinks...
Basically, I am receiving data from a source that I have no control over, and I need to transpose it into a suitable format for inserting into my database using CakePHP. So, here's how I'm doing it:
public function submitApp($data) {
$array = array(
'Student' => array(
'name' => $data['name'],
'email' => $data['email'],
'phone' => $data['phone'],
'address' => $data['address'],
'dob' => $data['dob'],
'gender' => $data['gender']
),
'Application' => array(
'course_id' => $data['course_id'],
'question1' => $data['question1'],
'question2' => $data['question2'],
'question3' => $data['question3'],
'question4' => $data['question4'],
),
'ApplicationQualification' => $data['Qualifications']
);
// Logic to save $array goes here
}
The problem is that sometimes not all of the keys in $data will be submitted to my app but I still want my app to work with what it gets.
I know that I can wrap each key in a conditional like this:
if (!isset($data['name'])) { $data['name'] = null; }
...and then building the array, but this seems like a pretty clumsy way of doing it. Is there a more efficient way to do this?
You could use a simple ternary statement
'name' => array_key_exists('name', $data) ? $data['name'] : null
Alternatively, you can set up a default array and then merge the given values in
$defaults = [
'name' => null,
'email' => null,
// etc
];
$data = array_merge($defaults, $data);
I am using $_POST to post data to a php file. In that php file, I have the following.
$params = array(
'name' => "$fname",
'email' => "$email",
'ad_tracking' => 'test',
'ip_address' => '$_SERVER["REMOTE_ADDR"]',
);
$subscribers = $list->subscribers;
$new_subscriber = $subscribers->create($params);
What is the best way to use the $_POST data to define the vales of each keys in the array?
The use of $_SERVER["REMOTE_ADDR"] is also not working as hoped.
POST variables are passed via the super global array $_POST in PHP. So in your case, this would technically work:
$params = array(
'name' => $_POST['fname'],
'email' => $_POST['email'],
'ad_tracking' => 'test',
'ip_address' => $_SERVER['REMOTE_ADDR'],
);
Your code for $_SERVER["REMOTE_ADDR"] was enclosed in single quotes, which in PHP means a verbatim string (i.e. without variable interpolation).
Btw, you should think of input filtering too - http://www.php.net/filter
To give you an example, this would perform input filtering in your current case:
$filtered = filter_input_array(INPUT_POST, array(
'fname' => FILTER_SANITIZE_STRING,
'email' => FILTER_VALIDATE_EMAIL,
);
Each value inside $filtered will either have a value (valid), be NULL (not present) or false (invalid).
Regarding "the use of $_SERVER["REMOTE_ADDR"] is also not working as hoped.":
Single-Quotes don't evaluate php variables
$params = array(
'name' => $_POST["fname"],
'email' => $_POST["email"],
'ad_tracking' => 'test',
'ip_address' => $_SERVER["REMOTE_ADDR"],
);
$subscribers = $list->subscribers;
$new_subscriber = $subscribers->create($params);