Codeigniter CSRF – how does it work

The CSRF token is added to the form as a hidden input only when the form_open() function is used.

A cookie with the CSRF token’s value is created by the Security class, and regenerated if necessary for each request.

If $_POST data exists, the cookie is automatically validated by the Input class. If the posted token does not match the cookie’s value, CI will show an error and fail to process the $_POST data.

So basically, it’s all automatic – all you have to do is enable it in your $config['csrf_protection'] and use the form_open() function for your form.

A good article I found that explains it very well: https://beheist.com/blog/csrf-protection-in-codeigniter-2-0-a-closer-look.html

Leave a Comment