1

次のコードを使用してデータベースにデータを追加できるようにしています$this->db->escape();が、htmlタグを追加でき、ビューで実行されるため、が機能していないようです:(

コード:

$this->form_validation->set_rules('aPartyLocation','A Party Location', 'required|trim|prep_for_form|max_length[35]|xss_clean');
        $this->form_validation->set_rules('aPartyPhone','A Party Phone', 'required|trim|numeric|max_length[35]|xss_clean');

        if($this->form_validation->run() === TRUE)
            {
                $userData = array(
                    'location' => $this->input->post('aPartyLocation', TRUE),
                    'phone' => $this->input->post('aPartyPhone', TRUE));

                $this->db->escape($userData);
                $this->party_model->addAParty($userData);

アップデート:

コントローラ:

$userData = array(
    'id' => $id,
    'location' => html_escape($this->input->post('aPartyLocation', TRUE)),
    'phone' => html_escape($this->input->post('aPartyPhone', TRUE))
    );  

モデル:

function addAParty($userData = NULL)
{
    $this->db->insert('aParty',$userData);
    return TRUE;
}
4

1 に答える 1

3

CodeIgniterのActiveRecordクラスを使用することをお勧めします。これにより、データが自動的にエスケープされます。

たとえば、挿入ステートメントは次のようになります。

$this->db->insert('yourTable',array(
                 'location' => $this->input->post('aPartyLocation',TRUE),
                 'phone' => $this->input->post('aPartyPhone')
           ));

2番目の引数は、キーがデータベースの列に対応する配列です。


編集

Active Recordは、SQLインジェクション攻撃のデータのみをサニタイズすると思います。2番目のパラメーターを$this->input->post()TRUEとして渡すと、XSS攻撃から保護されます。ただし、どちらもHTMLタグをエスケープしません。そのためには、htmlspecialchars関数を使用できます。

 $this->db->insert('yourTable',array(
                     'location' => htmlspecialchars($this->input->post('aPartyLocation',TRUE)),
                     'phone' => htmlspecialchars($this->input->post('aPartyPhone'))
                ));

$location = $this->input->post('aPartyLocation',TRUE);
$phone = $this->input->post('aPartyPhone');

 $this->db->insert('yourTable',array(
                     'location' => htmlspecialchars($location),
                     'phone' => htmlspecialchars($phone)
                ));
于 2012-06-14T10:03:57.787 に答える