首页 > 解决方案 > 用户输入值与数据库数组值未使用 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") {

我在这里想念什么?

标签: phpjsonwordpressvariables

解决方案


尝试将比较的值转换为整数,然后比较它们:

    if ( (int)$data['report'] !== (int)$code ) {
        wp_send_json_error();
    }

推荐阅读