codeigniter CSRF error: “The action you have requested is not allowed.”

心不动则不痛 提交于 2019-11-26 22:58:08

The problem solved by this Solution:

set $config['cookie_secure'] in config file to FALSE if you're using HTTP.

The easiest one for me was to whitelist the URI as explained in CodeIgniter User Guide (here)

Select URIs can be whitelisted from csrf protection (for example API endpoints expecting externally POSTed content). You can add these URIs by editing the ‘csrf_exclude_uris’ config parameter:

$config['csrf_exclude_uris'] = array('api/person/add');

Just Include this in your form and everything will be fine then.

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

if you allow true in $config['csrf_protection'] = true; within config file and you are also add autoload form than we can use.

Step 1. within config folder autoload file upload form helper

$autoload['helper'] = array('url', 'file','form');

Step 2.

$config['csrf_protection'] = true; 

Step 3. while uploading in view folder

<?php echo form_open_multipart('admin/file_upload'); ?>

Otherwise, you can use only

$config['csrf_protection'] = false;

In config/config.php I have

$config['csrf_token_name'] = 'my.token.name';

But when I use var_dump for $_POST I see:

 ["my_token_name"]=> string(32) "f5d78f8c8bb1800d10af59df8c302515"

CI change my csrf_token_name (sic!)

Solution: I changed

$config['csrf_token_name'] = 'my.token.name';

to

$config['csrf_token_name'] = 'my_token_name';

Now it works.

When all else failed, I noticed that I had my cookie variables set, removing cookie name, etc. resolved my issue.

To everyone who tried everything that was suggested here, and still has this problem.

My issue was the expiration time of the cookie.

$config['csrf_expire'] = 7200;

Afte the cookie expires and the user tries to submit an form, they will get the error

The action you have requested is not allowed.

I added a simple javascript to every page, which fixes the issue for 99% of your users. (the 1% being users who have JS disabled in their browser)

setInterval(function () {
  if(alert('Your session has expired!')){}
  else    window.location.reload(); 
}, 7200000);

In the config if you have set the cookie domain name

$config['cookie_domain']    = 'xyz.com';

and you browse using localhost. you will get the error

The action you have requested is not allowed

check that if helps

Make sure that your BASE_URL matches the URL that you are viewing. I have two aliases (one was created for oauth) and the project works on both aliases, but CSRF will fail if the BASE_URL doesn't match the URL in the browser.

EXCLUIR URL IN ARCHIVO CONFIG:

EJEMPLO:

$config['csrf_exclude_uris'] = array('main/registration','main/login');
Kelvin D souza

change line no 451

$config['csrf_protection'] = true;

to

$config['csrf_protection'] = false;

Because this csrf_protection is deprecated in CodeIgniter.

I've found a solution to this problem which is quite simple. I removed the div with the display:none style surrounding the csrf_protection input. The div is not relevant since the input type is set to hidden. In CodeIginiterFolder/system/helpers/form_helper.php, I changed the following content (around line 75) :

if (is_array($hidden) AND count($hidden) > 0)
{
    $form .= sprintf("<div style=\"display:none\">%s</div>", form_hidden($hidden));
}

for the following one :

if (is_array($hidden) AND count($hidden) > 0)
{
    $form .= form_hidden($hidden);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!