How to set csrf token globally in codeigniter like it is in laravel? like laravel uses csrf_token() in its meta tag, how can i use it in codeigniter ?
Asked
Active
Viewed 445 times
-2
-
This is **not** a duplicate question. The "duplicate" answer relates to Laravel and not to CodeIgniter. – DFriend Nov 21 '19 at 15:48
1 Answers
0
The Security Class as documented in the Codeigniter user guide outlines exactly this.
In a nutshell, you can get the CSRF token name and CSRF hash using:
$token = $this->security->get_csrf_token_name();
$hash = $this->security->get_csrf_hash();
To make use of this, you'd need to do something like this in your forms:
<input type="hidden" name="<?php echo $token; ?>" value="<?php echo $hash; ?>" />
However, unless you have a very specific use case, you do not need to set the CSRF token manually. CI's Form helper, when enabled, will automatically insert the CSRF token and hash if you create the form using form_open();

Javier Larroulet
- 3,047
- 3
- 13
- 30
-
But how can i set it for every single form in my application? i have more than 30 forms and modals – Sherwin Samuel Nov 21 '19 at 13:47
-
I see your point. CI doesn't work like Laravel in that sense. But replacing your opening ` – Javier Larroulet Nov 21 '19 at 14:02