title: CodeIgniter CSRF failing on forms
body: <p><strong>Note: This question is <em>not</em> about failing AJAX requests, it is about failing regular form submissions.</strong></p>
  
  <p>I have enabled CSRF protection in CodeIgniter 3, using the following <code>config.php</code>:</p>
  
  <pre><code>$config['csrf_protection'] = TRUE;
  $config['csrf_token_name'] = 'csrf_token_name';
  $config['csrf_cookie_name'] = 'csrf_cookie_name';
  $config['csrf_expire'] = 7200;
  $config['csrf_regenerate'] = TRUE;
  </code></pre>
  
  <p>I am correctly using <code>form_open()</code> in form fields in order to automatically generate the required hidden fields containing the CSRF tokens. However, when I submit any form on my website it results in the "An Error Was Encountered - The action you have requested is not allowed." error.</p>
  
  <p>Cookie settings:</p>
  
  <pre><code>$config['cookie_prefix'] = '';
  $config['cookie_domain'] = '';
  $config['cookie_path'] = '/';
  $config['cookie_secure'] = FALSE;
  $config['cookie_httponly'] = FALSE;
  </code></pre>
  
  <p>Session settings:</p>
  
  <pre><code>$config['sess_driver'] = 'database';
  $config['sess_cookie_name'] = 'ci_session';
  $config['sess_expiration'] = 2592000; // one month in seconds
  $config['sess_save_path'] = 'ci_sessions';
  $config['sess_match_ip'] = TRUE;
  $config['sess_time_to_update'] = 300;
  $config['sess_regenerate_destroy'] = FALSE;
  </code></pre>
  
  <p>Also, contrary to most questions about CI CSRF protection, CSRF tokens in my AJAX request <strong>do</strong> properly work by adding the following snippet to my initial JavaScript:</p>
  
  <pre><code>function getCookie(c_name) { // A javascript function to get the cookie value
      if(document.cookie.length &gt; 0) {
          c_start = document.cookie.indexOf(c_name + "=");
          if(c_start != -1) {
              c_start = c_start + c_name.length + 1;
              c_end = document.cookie.indexOf(";", c_start);
              if(c_end == -1) c_end = document.cookie.length;
              return unescape(document.cookie.substring(c_start,c_end));
          }
      }
      return "";
  }
  
  $.ajaxPrefilter(function(options, originalOptions, jqXHR){ // This function will attach "csrf_test_name" with all the request you are sending.
      if (options.type.toLowerCase() === "post") { // Required only if its a post method
          var csrf_token = getCookie("csrf_cookie_name");
          // initialize `data` to empty string if it does not exist
          options.data = options.data || "";
  
          // add leading ampersand if `data` is non-empty
          options.data += options.data?"&amp;":"";
  
          // add _token entry
          options.data += "csrf_token_name=" + csrf_token;
      }
  });
  </code></pre>
  
  <p>Any way to get my CSRF protection to work in form fields? Also, my <code>csrf_cookie_name</code> value in the cookie is different from the <code>csrf_token_name</code> value in the hidden HTML field, is this supposed to happen?</p>