1

背景と を使用Codeigniterしてフォーム処理を行います。でフォームが正常に検証されました。form_helperform_validationcontroller

model次に、クラスを使用してこのデータをデータベースに入れる必要があります。

仮定

フォームに複数の入力要素 (例: >20) があるとします。

質問 次のコード スニペットのうち、より効率的なのはどれですか?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);
}
4

3 に答える 3

2

[以下のベンチマークを見てください]の方$this->form_validation->set_value() 速い場合もありますが、これら 2 つの方法の最も重要な違いは、メソッドでXSS フィルタリング オプションを用意している$this->input->post()ことです。

フォーム検証 :: set_value() 機能

Form Validation Classでは、すべてのフィールドが$this->_field_dataプロパティに格納され、値は から$_POST直接取得され、$this->form_validation->set_value()メソッドは からデータを返す$this->_field_dataだけです。

入力 :: post() 機能

Input Classは XSS フィルタリング オプションを用意しています。これを使用して値をデータベースに保存することを検討してください。

注:メソッドは、特定のパラメーターなしで呼び出されない限り、デフォルトで全体ループしない
ことに注意してください。$this->input->post()$_POST$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で説明されているように、入力クラスが提供するセキュリティ フィルタリング操作が他にもあります。

于 2013-06-20T11:22:20.453 に答える
0

コード スニペット #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();
}

次に、この関数を呼び出して結果を照合します。

于 2013-06-20T17:37:35.920 に答える