【问题标题】:CodeIgniter: set_value() or post() - what is faster and what is the best practice to store data into databaseCodeIgniter:set_value() 或 post() - 什么更快,将数据存储到数据库中的最佳实践是什么
【发布时间】:2013-06-17 01:29:22
【问题描述】:

背景 使用Codeigniterform_helperform_validation 进行一些表单处理。 controller 中的表单已成功验证

现在我们需要使用model 类将这些数据放入数据库中。

假设

假设我们的表单有多个输入元素(例如 >20)。

问题 以下哪个代码 sn-ps 会更高效? Both snippets are obviously inside the controller method to which the form submits data.

代码片段 1

if ($this->form_validation->run())
{
    // validation successful, now collect the values in a variable to pass it to the model.
    $form_data['field1'] = $this->form_validation->set_value('field1');
    $form_data['field2'] = $this->form_validation->set_value('field2');
    // AND SO ON
    $form_data['fieldN'] = $this->form_validation->set_value('fieldN');

    // Now put this data into database.
    $this->corresponding_model->write_to_db($form_data);
}

代码片段 2

if ($this->form_validation->run())
{
    // validation successful, now collect the values in a variable to pass it to the model.
    $form_data['field1'] = $this->input->post('field1');
    $form_data['field2'] = $this->input->post('field2');
    // AND SO ON
    $form_data['fieldN'] = $this->input->post('fieldN');

    // Now put this data into database.
    $this->corresponding_model->write_to_db($form_data);
}

所以基本上我要问的是: 获取某些任意表单元素的发布数据更好? $this->input->post$this->form_validation->set_value()

PS:如果我们查看代码中的set_value()post() 函数(请参见下文),显然set_value() 会更快,因为post() 循环通过整个$_POST。所以从某种意义上说,这也是关于什么是最佳实践?

Form_validation.php,set_value() 方法

public function set_value($field = '', $default = '')
{
    if ( ! isset($this->_field_data[$field]))
    {
        return $default;
    }

    // If the data is an array output them one at a time.
    //     E.g: form_input('name[]', set_value('name[]');
    if (is_array($this->_field_data[$field]['postdata']))
    {
        return array_shift($this->_field_data[$field]['postdata']);
    }

    return $this->_field_data[$field]['postdata'];
} 

Input.php,post() 方法

function post($index = NULL, $xss_clean = FALSE)
{
    // Check if a field has been provided
    if ($index === NULL AND ! empty($_POST))
    {
        $post = array();

        // Loop through the full _POST array and return it
        foreach (array_keys($_POST) as $key)
        {
            $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
        }
        return $post;
    }

    return $this->_fetch_from_array($_POST, $index, $xss_clean);
}

【问题讨论】:

    标签: php forms codeigniter validation codeigniter-2


    【解决方案1】:

    如果规则已在输入上运行,则两个函数都将返回修改后的值。

    当您想从表单中读取帖子值时,使用 $this->input->post()

    set_value() 用于重新填充验证失败的表单。 它没有额外的过滤,所以它更快,但我更喜欢你应该使用$this->input->post() 来保证安全。

    【讨论】:

    • 第一: set_value() 永远不会修改值,它只是返回字段from $_POST 的值。
    • 第二:如果你想读取一个帖子值可以直接使用$_POST[$field],不一定是$this->input->post()
    • 第三个: set_value() 将返回值即使表单验证运行没有错误,在性能方面它与$this->input->post() 相比没有任何优势,除非您在 post() 方法上启用 XSS 过滤。
    【解决方案2】:

    虽然$this->form_validation->set_value() 在某些情况下可能更快,[看看下面的基准测试],这两种方法之间最重要的区别是准备一个 XSS 过滤选项 在$this->input->post() 方法中。

    表单验证 :: set_value() 功能

    Form Validation Classall fields are stored in $this->_field_data属性中,值直接来自$_POST$this->form_validation->set_value()方法只是returns data from $this->_field_data

    输入 :: post() 功能

    Input Class 准备了一个 XSS 过滤选项,您可以考虑使用它来将值存储到数据库中。

    注意:
    请注意$this->input->post() 方法does NOT loop through the entire $_POST by default,除非在没有特定$index 参数的情况下调用它。

    基准测试

    系统信息:

    CPU: Intel Core-i5 760 @ 2.80 GHz RAM: 2.00 GB

    测试用例: 一个 30 个字符的字符串 文本字段。

    set_rules()                   0.0000
    Check Validation              0.0003
    set_value()                   0.0000
    Form Validation (Overall)     0.0024
    
    post() without XSS filtering  0.0000
    post() with XSS filtering     0.0002
    

    结论

    如果您需要在将值存储到数据库之前执行 XSS 过滤,我建议使用 CodeIgniter Input 类。此外,输入类提供更多安全过滤操作,在CodeIgniter User Guide 中进行了解释。

    【讨论】:

      【解决方案3】:

      有时编码 sn-p #1,有时 - #2。在大多数情况下 $this->input->post() 更快。但它可能取决于环境代码和数据。您可以轻松检查哪种情况更快:

      public function codeSnippet1(){
      ob_start();
      $this->output->enable_profiler(TRUE);
      // your code here
      
      ob_end_flush();
      }
      
      public function codeSnippet2(){
      ob_start();
      $this->output->enable_profiler(TRUE);
      // your code here
      
      ob_end_flush();
      }
      

      然后调用这个函数并匹配结果。

      【讨论】:

        猜你喜欢
        • 2011-12-03
        • 2011-02-14
        • 1970-01-01
        • 2015-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-12
        • 2011-07-25
        相关资源
        最近更新 更多