CSRF Codeigniter 3验证

时间:2017-08-13 02:32:10

标签: php codeigniter csrf codeigniter-3 csrf-protection

我一直在阅读有关codeigniter中crsf保护的内容,但在配置文件中启用csrf后,我似乎无法找到一个关于如何继续的正确教程。

我有一个由控制器函数users/create生成的表单,该表单提交给另一个函数users/submit_new

我使用了表单助手类,以便自动生成crsf字段。

我在提交功能上有这个验证功能:

if ($this->input->post(get_csrf_token_name()) == get_csrf_hash()) {
$this->users_model->create(); }

但我得到的是操作不允许错误。 验证csrf的正确方法是什么?或者我做错了什么?

1 个答案:

答案 0 :(得分:0)

如果您按照用户指南提及的方式使用CodeIgniter CSRF,请设置:

$config['csrf_protection'] = TRUE; // this in application/config/config.php

并且您还使用表单助手生成表单打开标记,然后您不需要检查令牌和散列方式。 CodeIgniter为您完成此任务。

阅读文档:https://www.codeigniter.com/user_guide/libraries/security.html#cross-site-request-forgery-csrf

如果您仍有问题,请参阅相关问题:

codeigniter CSRF error: "The action you have requested is not allowed."

Action you have requested is not allowed error