Welcome Guest, Not a member yet? Register   Sign In
form submit with csrf not working
#4

(This post was last modified: 11-17-2021, 07:25 PM by includebeer.)

(11-16-2021, 10:50 PM)coderscvoen Wrote: When i submit the form; i do not get any feedback from the flash messages have set.

I'm not sure if I understand correctly, but if the CSRF validation fail, it will automatically set an "error" flash message and redirect to the previous page. So your view should echo the error variable if present:
https://codeigniter.com/user_guide/libra...on-failure
Quote:When a request fails the CSRF validation check, it will redirect to the previous page by default, setting an error flash message that you can display to the end user.

(11-17-2021, 02:59 AM)captain-sensible Wrote: <?=form_open('contact') ?>
<?= csrf_field() ?>
// here i manually put in csrf to be generated, but i've read if you use appropriate helper  and the appropriate form open eg: <?=form_open('contact') ?>
you don't even need to put in the        <?= csrf_field() ?>    I'm of course a control freak so i like to do things my way.

You said it yourself, open_form() already add the CSRF field in your form. You don't need to add it again with csrf_field(). Maybe you like to do things your way, but then you'll break your site and waste time trying to understand why it doesn't work. If you don't trust the documentation, you can always look at the code to see what it really do: https://github.com/codeigniter4/CodeIgni...er.php#L62
PHP Code:
// Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
        $before Services::filters()->getFilters()['before'];

        if ((in_array('csrf'$beforetrue) || array_key_exists('csrf'$before)) && strpos($actionbase_url()) !== false && ! stripos($form'method="get"')) {
            $form .= csrf_field($csrfId ?? null);
        

It is always a good idea to study the framework's code. Now you can see what is happening and you can learn some interesting things. Like here, I didn't know it doesn't add the csrf field if it's a GET method instead of a POST!
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply


Messages In This Thread
RE: form submit with csrf not working - by includebeer - 11-17-2021, 07:13 PM



Theme © iAndrew 2016 - Forum software by © MyBB