Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
403 forbidden even I put csrf name and value in form

I am turning CSRF security on in my code:

$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;
$config['csrf_regenerate'] = TRUE;
$config['csrf_exclude_uris'] = array();

Now, I am adding a hidden field in my form:

<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">

In my form post, I am getting the data:

csrf_test_name:50711c69fc79244945f0aaecf7ac1aa4
teacher_name:
teacher_mobile:
teacher_email:
teacher_about:

But, still I am getting errors like:

<h1>An Error Was Encountered</h1>
        <p>The action you have requested is not allowed.</p>    </div>

I am sending data using ajax call:

submitHandler: function (form) {
            var URL = $("#teacherForm").attr("action");
            var METHOD = $("#teacherForm").attr("method");
            $.ajax({
                type: METHOD,
                url: URL,
                data: $(form).serialize(),
                success: function (data) {
                    var data = $.parseJSON(data);
                     ......
                     ......

What am I doing wrong?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I suggest you change one CSRF config setting:

$config['csrf_regenerate'] = FALSE;

and see if that helps.

If you want to regenerate the hash on each POST and use AJAX you will need to return the new hash to the browser and update the CSRF hidden field values so the next POST will pass the security check.

You would be wise to use the Form Helper instead of manually creating the hidden CSRF input. In a view that would look like

echo form_open('controller/method');

Documentation on form_open() here.

Read about how using form_open() add the hidden CSRF here.

over 4 years ago · Santiago Trujillo Report

0

Once try to add csrf in ajax request not in form.Its worked for me

Here is the code :

submitHandler: function (form) {
            var URL = $("#teacherForm").attr("action");
            var METHOD = $("#teacherForm").attr("method");
var data= $(form).serialize();
var post_data = {
                        'result': data,
  '<?php echo $this->security->get_csrf_token_name(); ?>': '<?php echo $this->security->get_csrf_hash(); ?>'
                    };
            $.ajax({
                type: METHOD,
                url: URL,
                data:post_data,
                success: function (data) {
                    var data = $.parseJSON(data);
                     ......
                     ......
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!