CodeIgniter 仅在某些页面中使用 CSRF 保护 [英] CodeIgniter use CSRF protection only in some pages

查看:29
本文介绍了CodeIgniter 仅在某些页面中使用 CSRF 保护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是保护一些敏感表单免受 codeigniter 中的 CSRF 攻击,但不是所有页面.

What I want to do is to protect some sensitive forms from CSRF attack in codeigniter but not all pages.

为了防止 CSRF 如果我在 config.php 中设置它,它适用于所有页面.有没有办法通过在控制器中设置来仅对某些页面执行此操作?

To protect from CSRF if I set it in config.php it applies for all pages. is there any way to do that only for some pages by setting in controller?

$config['csrf_protection'] = TRUE;

推荐答案

您可以通过编辑 config.php 文件来做到这一点

You can do this by editing the config.php file

 $config['csrf_protection'] = FALSE;

步骤 1:创建要保护的页面数组

Step 1: create an array of pages that you want to protect

例如.$csrf_pages = array('login','test');

Step2:检查是否有对受保护页面的任何请求,然后将其设置为TRUE;

Step2: check if there is any request for the protected page then set it to TRUE;

if (isset($_SERVER["REQUEST_URI"])) {
    foreach ($csrf_pages as $csrf_page){
        if(stripos($_SERVER["REQUEST_URI"],$csrf_page) !== FALSE) {
            $config['csrf_protection'] = TRUE;
            break;
        }
    }

}

第 3 步:将此添加到您的视图中

Step 3: add this to your views

<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash();?>" />

或者简单地使用 form_open() 函数自动添加隐藏的 CSRF 令牌字段.

Or simply use the form_open() function to add the hidden CSRF token field automatically.

这篇关于CodeIgniter 仅在某些页面中使用 CSRF 保护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆