2015-03-02 141 views
-2

我不想更改配置文件中的默认设置,默认情况下它是FALSE。但我想唯一的联系形式使用CSRF(其他形式的不需要):Codeigniter:csrf不工作是这种情况

这是我的控制器:

class Welcome extends CI_Controller { 
    public function __construct() { 
     parent::__construct(); 
     $this->load->helper('form_helper'); 
     $this->load->library('session'); 
    } 

    public function index() 
    { 
     $this->config->set_item('csrf_protection', TRUE); 
     $this->load->view('welcome_message'); 
    } 
} 

结束我的观点:

<div id="container"> 
    <?php echo form_open(NULL); ?> 

    </form> 
</div> 

但CSRF不工作,它总是空值:

enter image description here

+0

你有没有设置在您的配置文件中的加密密钥? – AdrienXL 2015-03-02 17:53:11

+0

这似乎是框架中的一个错误。无论如何,在所有表单上都应该启用CSRF。 – Philip 2015-03-02 18:52:51

+0

我在你的配置文件中设置了加密密钥'$ config ['encryption_key'] ='tkppinpin';' – 2015-03-03 02:30:38

回答

0

你对C使能CSRF每个窗体上的onfig.php如果启用csrf必须使用codeigniter窗体帮助函数。

http://www.codeigniter.com/user_guide/libraries/security.html

http://www.codeigniter.com/user_guide/helpers/form_helper.html

变化

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

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

的application/config/config.php文件

$config['csrf_protection'] = TRUE; 
$config['csrf_token_name'] = 'token'; 
$config['csrf_cookie_name'] = 'cookie_monster'; 
$config['csrf_expire'] = 7200; 

我不认为可以使用这种方式。$this->config->set_item('csrf_protection', TRUE);

每一表格必须使用<?php echo form_open('yourcontrollerfunction');?>

如果像你返回空必须控制器的名字你可能需要包括的index.php如果没有设置htaccess的或路线..

<?php echo form_open('your_controller_name_or_function');?>

<?php echo form_close();?>

<?php echo form_open('index.php/your_controller_name_or_function');?>

<?php echo form_close();?>