Welcome Guest, Not a member yet? Register   Sign In
Custom error message for Csrf validation
#1

Hi,
how can we have custom error message displayed when the CSRF validation fails instead of the exception thrown usually?
Thanks.
Reply
#2

See https://codeigniter4.github.io/CodeIgnit...on-failure
Reply
#3

(11-18-2023, 06:58 PM)kenjis Wrote: See https://codeigniter4.github.io/CodeIgnit...on-failure

I don't want to redirect back. I just want to display a simple message when the csrf request has failed.
Reply
#4

Try/catch the SecurityException and show an error message you want.
Reply
#5

(11-19-2023, 12:59 AM)kenjis Wrote: Try/catch the SecurityException and show an error message you want.

For some reason I cannot catch the exception. I tried with the bellows but without result.

try {

...

} catch (SecurityException $e) { / } catch (\SecurityException $e) {


}
Reply
#6

Check the CI4 source code. The classname is \CodeIgniter\Security\Exceptions\SecurityException.
Reply
#7

(This post was last modified: 11-24-2023, 06:04 PM by semprom.)

(11-24-2023, 05:49 PM)kenjis Wrote: Check the CI4 source code. The classname is \CodeIgniter\Security\Exceptions\SecurityException.

I tried also that, it didn't work.
Reply
#8

If you try to catch the exception in your controller, sorry you can't.
Because CSRF filter throws the exception. The controller is not executed yet.

Customize CSRF filter.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB