-2

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 ?

Touheed Khan
  • 2,149
  • 16
  • 26
Sherwin Samuel
  • 41
  • 1
  • 1
  • 4
  • This is **not** a duplicate question. The "duplicate" answer relates to Laravel and not to CodeIgniter. – DFriend Nov 21 '19 at 15:48

1 Answers1

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