CodeIgniter 仅在某些页面中使用 CSRF 保护 [英] CodeIgniter use CSRF protection only in some pages
问题描述
我想要做的是保护一些敏感表单免受 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屋!