php - 用户输入值与数据库数组值未使用 JSON 进行验证
问题描述
大量测试后更新的问题
这是一个奇怪的问题,但希望有人能指出这个缺陷。
底部“3333”的背景框中显示的值是从数据库中提取的 post_meta 数组中的值。
数组 (custom_group) 如下所示:
a:5:{s:23:"custom1";s:2:"15";s:23:"custom_value";s:1:"5";s:14:"custom3";s:1:" 8";s:14:"custom4";s:11:"免费咖啡";s:12:"custom_code";s:4:"3333";}
我使用这个函数提取它:
public static function get_business_vars($key)
{
global $post;
$results = get_post_meta($post->ID, 'custom_group', true);
return $results[$key];
}
$code = get_business_vars('custom_code')
警报中的值是用户输入的值,通过以下 JSON 检查发送:
if ( check_ajax_referer( 'nonce_' . $data['post_id'], 'nonce', false ) == false ) {
wp_send_json_error();
}
//check entry against validation
$code = get_business_vars('custom_code');
if ( $data['report'] !== $code) {
wp_send_json_error();
} else {
wp_send_json_success();
}
因此,即使 POST 数据显示报告为 3333 并且变量 $code 回显 3333,我也只会收到 JSON 错误消息。
这甚至返回“是”:
if (get_business_vars('custom_code') == 3333) {
echo "yes";
} else {
echo "no";
}
和
if ( strval($code) !== strval(intval($code)) ) {
echo "Your variable is not an integer";
} else {
echo "yes it is";
}
打印:“是的”</p>
我可以让 JSON 进行验证的唯一方法是使用:
if ( $data['report'] !== "3333") {
我在这里想念什么?
解决方案
尝试将比较的值转换为整数,然后比较它们:
if ( (int)$data['report'] !== (int)$code ) {
wp_send_json_error();
}
推荐阅读
- python - 从父目录运行烧瓶脚本
- python - 修改代码,以便您自己的名字可以包含在最后一个帕萨迪纳地块的标题中
- python - 我有来自 html 'copy element' 的一行,我正在尝试制作一个正则表达式来去除不相关的内容并保留列表
- ansible - ansible playbook 在第二个循环中访问第一个循环变量
- php - 如何在codeigniter中显示数据数组?
- javascript - 与 Ruby 相比,在 Javascript 中,在遍历数组时,您是否不需要“保护”以防止迭代器越界?
- javascript - 将 Cookie 添加到 API
- java - 递归遍历java中的矩阵以找到最佳路径
- for-loop - 基于生成行以在数据框的列中创建序列
- c - 如何在 C 中实现多个定时器?