codeigniter以安全的方式发布值

时间:2014-11-09 15:22:45

标签: php codeigniter

我将带有AJAX POST方法的id传递给我的Codeigniter控制器:

我在配置文件中设置了$config['global_xss_filtering'] = TRUE;

$id = $this->input->post('id');
$this->model_a->did($id);

我想知道上面的代码是否足够安全,或者我应该添加这样的代码:

if ($this->input->post('id') && !empty($_POST['id'])) {

$id = $this->input->post('id');

if (is_int($id)) {
 $this->model_a->did($id);

  }
}

或许我应该添加别的东西?能帮我找到最安全的方式。

更新

并且下面提到的代码对于通过html表单提交的值是否足够安全?

 $this->form_validation->set_rules('username', 'Username', 'required|trim');

if ($this->form_validation->run()) {

    $username = $this->input->post('username');
}

我应该添加if ($this->input->post('username') && !empty($_POST['username']))还是别的什么?

1 个答案:

答案 0 :(得分:1)

global_xss_filtering只是转义(或转换)某些“危险”的html标签。

因为id总是一个整数,所以使用你提到的检查/验证会更安全

if($this->input->post('id') && !empty($_POST['id']))
{

 $id = $this->input->post('id');
 if(is_int($id)) 
  {
     $this->model_a->did($id);
  }

}

OR

if ($this->input->post('id') && !empty($_POST['id'])) 
{
  $id = (int)$this->input->post('id');
  $this->model_a->did($id);

}

关于问题的更新部分 -

当您使用codeigniter表单验证时,我认为不需要使用额外的检查/验证。你可以使用下面给出的东西 -

$this->form_validation->set_rules('username', 'Username', 'required|trim');




 if ($this->input->server('REQUEST_METHOD') === 'POST') //To determine if a form has been submitted
{
   if ($this->form_validation->run()) {

    $username = $this->input->post('username');
    //other fields will go here
 }


 }

OR

if ($_POST) //To determine if a form has been submitted
{
  if ($this->form_validation->run()) {

    $username = $this->input->post('username');
    //other fields will go here
   }
}