简体   繁体   English

如何快速检查CodeIgniter中的GET和POST变量是否同时设置和为数字?

[英]How can I quickly check if GET and POST variables in CodeIgniter are both set and numeric?

How can I quickly validate if a GET or POST variable in CodeIgniter is both set and numeric for use as error or status messages in views? 我如何快速验证CodeIgniter中的GETPOST变量是否同时设置为数字和用作视图中的错误或状态消息?

It is very tedious to do something like this each time each time I want to check variables: 每次我想要检查变量时,每次执行这样的操作非常繁琐:

if ($this->input->get('error', True)) {
    if (is_numeric($this->input->get('error', True))) {
        $data['error'] = $this->input->get('error', True);
    }
}

get_numeric_input() for CodeIgniter 用于CodeIgniter的get_numeric_input()

mixed get_numeric_input ( string $name [, bool $required = True [, string $source = "GET" [, bool *$xss_clean* = True ]]] ) 混合get_numeric_input (字符串$ name [,bool $ required = True [,字符串$ source = “ GET” [,bool * $ xss_clean * = True ]]])

Below is a function that I created because I was tired of checking if GET and POST variables existed and were numeric. 下面是我创建的函数,因为我厌倦了检查GET和POST变量是否存在以及是否为数字。

This was mainly used when handling errors or status messages, because I could use redirect("original_page.php?error=1"); 这主要用于处理错误或状态消息,因为我可以使用redirect("original_page.php?error=1"); to pass an error to the original page. 将错误传递给原始页面。 On the original page, I could simply do if (isset($error)) { … } and display a message depending on the value. 在原始页面上,我可以简单地执行if (isset($error)) { … }并根据值显示一条消息。 However, it was necessary to check these variables before sending them to the view in the interest of security. 但是,出于安全考虑,有必要在将它们发送到视图之前检查这些变量。 This process proved to be quite repetitive and tedious. 事实证明,这一过程非常重复且乏味。

This function below is to be added to the bottom of wwwroot/application/system/core/Input.php 下面的此功能将添加到wwwroot/application/system/core/Input.php

It is to be used as follows: 它的用法如下:

Example 1: 范例1:

function index() {
   if ($error = $this->input->get_numeric_input('error', True, "GET", True)) {
      $data['error'] = $error;
   }
}

In this example, if $_GET['error'] is both set and numeric, it will set $data['error'] to that value. 在此示例中,如果同时设置了$_GET['error']和数值,则它将$data['error']设置为该值。 If it is either not set and/or not numeric, it will terminate the script. 如果未设置和/或不是数字,它将终止脚本。

Example 2: 范例2:

function index() {
   if ($error = $this->input->get_numeric_input('error', False, "POST", True)) {
      $data['error'] = $error;
   }
}

In this example, if $_POST['error'] is both set and numeric, it will set $data['error'] to that value. 在此示例中,如果同时设置了$_POST['error']和数值,则它将$data['error']设置为该值。 If it is either not set and/or not numeric, it will continue and not set any values in the $data array. 如果未设置和/或未设置数字,则它将继续并且不会在$ data数组中设置任何值。

The first argument is the variable name to be checked. 第一个参数是要检查的变量名。 The second variable is the boolean that makes the check required or not. 第二个变量是布尔值,该布尔值是否要求进行检查。 If you have this set to TRUE, then if the variable is not set OR if it is not numeric, it will show an error and immediately terminate the script. 如果将其设置为TRUE,则如果未设置变量,或者如果变量不是数字变量,它将显示错误并立即终止脚本。 If set to False, then it will will simply return False, and the script will move on. 如果设置为False,则它将仅返回False,脚本将继续运行。 The third variable is either POST or GET, and will determine if the function looks for the variable in the $_GET or $_POST arrays. 第三个变量是POST或GET,它将确定函数是否在$ _GET或$ _POST数组中查找变量。 Finally, the fourth variable indicated whether or not the values will be XSS_CLEAN when returned. 最后,第四个变量指示返回时的值是否为XSS_CLEAN。

NOTE: Both the second, third, and fourth arguments are optional, and default to True, “GET,” and True, respectively. 注意:第二,第三和第四个参数都是可选的,默认分别为True,“ GET”和True。

Here is the code: 这是代码:

function get_numeric_input($name, $required = True, $source = "GET", $xss_clean = True) {
    if ($source === "GET") {
        if ($this->get($name, $xss_clean)) {
            if (is_numeric($this->get($name, $xss_clean))) {
                return $this->get($name, $xss_clean);
            } else {
                if ($required) {
                    show_error("$source variable $name is not numeric!");
                    log_message('error', "$source variable $name is not numeric!");
                    return False;
                } else {
                    return False;
                }
            }
        } else {
            if ($required) {
                show_error("$source variable $name is not set!");
                log_message('error', "$source variable $name is not set!");
                return False;
            } else {
                return False;
            }
        }
    } elseif ($source === "POST") {
        if ($this->post($name, $xss_clean)) {
            if (is_numeric($this->post($name, $xss_clean))) {
                return $this->post($name, $xss_clean);
            } else {
                if ($required) {
                    show_error("$source variable $name is not numeric!");
                    log_message('error', "$source variable $name is not numeric!");
                    return False;
                } else {
                    return False;
                }
            }
        } else {
            if ($required) {
                show_error("$source variable $name is not set!");
                log_message('error', "$source variable $name is not set!");
                return False;
            } else {
                return False;
            }
        }
    }
}

A possible alternative is to extend the form validation so that you would have a way to validate $_GET aswell. 一种可能的选择是扩展表单验证,以便您也可以验证$ _GET。 Using the form validation library does save time imo (an extended version - fit to your needs - is advisable). 使用表单验证库确实可以节省imo时间(建议使用扩展版本-适合您的需求)。 CodeIgniter Validation: possible to validate GET query strings? CodeIgniter验证:可以验证GET查询字符串吗? talks about this. 谈论这个。

Just use an intermediary variable, for a short and fast code: 只需使用中间变量,即可获得简短且快速的代码:

$input_error = $this->input->get('error');
$data['error'] = ctype_digit($input_error) ? $input_error : FALSE;


If you really want a one-liner: 如果您真的想要单线:

function validate_integer_input($input) {
    return ctype_digit($input) ? $input : FALSE;
}

$data['error'] = validate_integer_input($this->input->get('error'));


  • $data['error'] will always be set, which is a good thing, because $data will always be set in your view, so you can simply do if ($data) instead of if (isset($data)) . $data['error']将始终被设置,这是一件好事,因为$data将始终在您的视图中设置,因此您可以简单地执行if ($data)而不是if (isset($data))
  • When dealing with GET and POST input you have to know some aspects of typing. 在处理GET和POST输入时,您必须了解打字的某些方面。 For the most important: 最重要的是:
    • A GET/POST input, of course if it is set, is always of type string. GET / POST输入(当然如果已设置)始终为字符串类型。
    • Only '' (empty) and '0' strings evaluate to FALSE, all other values evaluate to TRUE. 只有'' (空)和'0'字符串的值为 FALSE,所有其他值的值为TRUE。
    • ctype_digit() expects a string, but this code may pass it FALSE (from CI->input). ctype_digit()需要一个字符串,但是此代码可以将其传递为FALSE(来自CI-> input)。 But it's fine, as FALSE casts to an empty string. 但这很好,因为FALSE 强制转换为空字符串。


As a side note, XSS filtering is not needed for this case. 附带说明,在这种情况下,不需要XSS过滤。

  • XSS filtering has quite a performance impact and should be activated only when needed. XSS过滤对性能有很大影响,应仅在需要时才激活。 A rule of thumb is that the filtering is needed for data which is displayed or included wherever in the HTML source. 一条经验法则是,无论在HTML源代码中的何处显示或包含的数据都需要过滤。
  • For this case, we already made sure the input can only contain digits, so we're safe. 对于这种情况,我们已经确保输入只能包含数字,因此很安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM