gpt4 book ai didi

php - 删除除我想要的所有数组元素吗?

转载 作者:IT王子 更新时间:2023-10-28 23:57:44 25 4
gpt4 key购买 nike

我有一个从 HTML 表单获取 post 参数的 Controller ,然后它将它们发送到模型,该模型将数组插入到 Cassandra 数据库中。

它是 SQLInjection proof,因为它是 NoSQL,但是我担心用户可以模拟 100k post 参数或者只添加一些我不需要的参数,它会被插入到数据库中。我如何才能确保只有我需要的值才会保留在我的数组中。

例子:

$post = ['parent_id', 'type', 'title', 'body', 'tags']; // Good
$post = ['parent_id', 'type', 'title', 'body', 'tags', 'one', 'two', 'three'] // Bad

我如何确保我的数组将取消设置所有不在好的示例中的元素?

最佳答案

通过将您确实期望的条目列入白名单。

<?php
$post = array(
'parent_id' => 1,
'type' => 'foo',
'title' => 'bar',
'body' => 'foo bar',
'tags' => 'foo, bar',
'one' => 'foo',
'two' => 'bar',
'three' => 'qux'
);

$whitelist = array(
'parent_id',
'type',
'title',
'body',
'tags'
);

$filtered = array_intersect_key( $post, array_flip( $whitelist ) );

var_dump( $filtered );

无论如何,使用 Cassandra 作为数据存储当然不是不对接收到的数据进行验证的理由。

关于php - 删除除我想要的所有数组元素吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10122801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com