For codeigniter4 You can enable CSRF protection by altering your app/Config/Filters.php
and enabling the csrf filter globally:
public $globals = [
'before' => [
//'honeypot'
'csrf'
]
];
Change the name here app/Config/App.php
/*
|--------------------------------------------------------------------------
| Cross Site Request Forgery
|--------------------------------------------------------------------------
| Enables a CSRF cookie token to be set. When set to TRUE, token will be
| checked on a submitted form. If you are accepting user data, it is strongly
| recommended CSRF protection be enabled.
|
| CSRFTokenName = The token name
| CSRFHeaderName = The header name
| CSRFCookieName = The cookie name
| CSRFExpire = The number in seconds the token should expire.
| CSRFRegenerate = Regenerate token on every submission
| CSRFRedirect = Redirect to previous page with error on failure
*/
//public $CSRFTokenName = 'csrf_test_name';
public $CSRFTokenName = 'form_csrf';
public $CSRFHeaderName = 'X-CSRF-TOKEN';
public $CSRFCookieName = 'csrf_cookie_name';
public $CSRFExpire = 7200;
public $CSRFRegenerate = true;
public $CSRFRedirect = true;
If you use the form helper, then form_open()
will automatically insert a hidden csrf field in your forms. If not, then you can use the always available csrf_token()
and csrf_hash()
functions
helper('form');//::::Load form helper
echo form_open('/u/sign-up', ['csrf_id' => 'my-id']);
will return:
<form action="http://example.com/index.php/u/sign-up" method="post" accept-charset="utf-8">
<input type="hidden" id="my-id" name="form_csrf" value="964ede6e0ae8a680f7b8eab69136717d" />