Php codeigniter以安全的方式发布值
我使用AJAX POST方法将id传递给我的Codeigniter控制器: 我已将Php codeigniter以安全的方式发布值,php,codeigniter,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'
$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');
}
我应该添加如果($this->input->post('username')&&!empty($\u post['username'))
或其他什么吗?全局过滤只是转义(或转换)某些“危险的”html标记
由于id始终是一个整数,因此使用您提到的检查/验证将更加安全
if($this->input->post('id') && !empty($_POST['id']))
{
$id = $this->input->post('id');
if(is_int($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
}
}
或
只要xss_过滤处于启用状态以防止CSRF
攻击,您的代码就可以了。我更喜欢您编写的第二个示例,它确保提交一个值,并且在尝试执行任何操作之前,该值实际上是一个整数。谢谢您的回答。你能检查一下我问题的更新部分吗?谢谢你的回答。你能检查一下我问题的更新部分吗?
$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
}
}
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
}
}