2015-03-02 139 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();?>