如何在 CodeIgniter 3 中启用 CSRF 保护并使用 `get_csrf_hash()` 函数?

问题描述 投票:0回答:1

我正在尝试在我的 CodeIgniter 3 应用程序中启用 CSRF 保护。但是,当我尝试在我的视图文件中使用

get_csrf_hash()
函数时,它没有返回值。我该如何解决这个问题?

在CodeIgniter 3中使用

$this->security->get_csrf_token_name()
时,只会生成CSRF token字段的name属性值,而使用
$this->security->get_csrf_hash()
不会生成value属性。

php security deployment codeigniter-3 csrf
1个回答
0
投票

要在您的 CodeIgniter 3 应用程序中启用 CSRF 保护,您需要执行以下操作:

  1. 打开位于

    config.php
    目录中的
    application/config/
    文件。

  2. $config['csrf_protection']
    设置为TRUE以启用CSRF保护:

    $config['csrf_protection'] = TRUE;

  3. 保存

    config.php
    文件。

注意 您需要在控制器中加载表单助手和安全助手,然后才能分别使用

form_hidden()
函数以及
get_csrf_token_name()
get_csrf_hash()
函数:

$this->load->helper('form');

$this->load->helper('security');

© www.soinside.com 2019 - 2024. All rights reserved.