Site icon Cody Paste

csrf in CodeIgniter 4

CSRF/csrf in ci4

  1. Enable CSRF:
    Go to app/Config/Filters.php 
    Search for public array $methods and replace with:

     
  2. Use (Just paste under <from>) :
    <input type=”hidden” name=”<?= csrf_token() ?>” value=”<?= csrf_hash() ?>” /> 

 

=============================================

CSRF in AJAX CI4

Add this in <form> or without form, it will get it by id

AJAX Script for Handling

Retrieve the CSRF token from the <input> field and include it in the AJAX request. After receiving the response, update the token dynamically.

Controller to Handle the Request

The controller processes the request and returns a response with an updated CSRF token.

 

Exit mobile version