Codeigniter get_csrf_hash()返回空

时间:2017-06-26 21:46:27

标签: php mysql codeigniter validation codeigniter-3

我尝试用Codeigniter Security Class进行实现,因为我想在这个框架上实现任何好的功能,以避免我的网站遭到黑客入侵的SQL注入和恶意攻击。

<小时/> 的问题:

为什么当我尝试回显或print_r this get_csrf_hash()时,它只是没有出现任何内容。

我当前的代码

我的控制器

<?php

class Myclass extends CI_Controller {

  function __construct() {

    parent::__construct();

    $this->load->library('session');
    $this->load->helper('security');
    $this->load->helper('form');

    $this->my_simple_login->chek_login();

  }

  public function index(){

     $this->load->view('/myform');

  } // end index function (show form)

  function submit_form(){

    print_r( $this->input->post('my_input') ); // YEAH

    print_r( $this->input->post('csrf_test_name') ); //empty??

    print_r( $this->security->get_csrf_hash() ); //empty??

  } //end form submitted

} // end class

我的观点

        <?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    ?><!DOCTYPE html>
    <head></head><body>

<?php
          $attributes = array('class' => 'my_html_class'); // add class to form HTML 
          echo form_open_multipart('myclass/submit_form', $attributes);
          ?>

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

          <input name="my_input" value="YEAH"/>

          <button type="submit" >SUBMIT FORM</button>

          <?php echo form_close(); ?></body></html>

我的CI配置

$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;
$config['csrf_regenerate'] = TRUE;
$config['csrf_exclude_uris'] = array();

1 个答案:

答案 0 :(得分:0)

这个问题已经解决了。因为我没有在Codeigniter配置文件中将TRUE设置为$config['global_xss_filtering']

$config['global_xss_filtering'] = TRUE;