表单提交时出现代码点火器 CSRF 错误


codeigniter csrf error on form submission

>我有一个使用代码点火器括号的表单

echo form_open('signup');
echo form_close();

当我提交它时,我收到以下错误

An Error Was Encountered
The action you have requested is not allowed.

不是always,而是经常...

即使表单中存在隐藏的输入字段:

<div style="display:none">
<input type="hidden" value="token name is here" name="csrf_token_name">
</div>

这也发生在类似的表单上(登录)

编辑:通过表单生成的html

<form accept-charset="utf-8" method="post" action="http://www.example.com/signup">
<div style="display:none">
<input type="hidden" value="93565fb5855d31af3d46bd655b11a4a6" name="csrf_token_name">
</div>
<input id="username" type="text" placeholder="Username" maxlength="20" value="" name="username">
<input id="email" type="text" placeholder="Email" value="" name="email">
<input id="password" type="password" placeholder="Password" value="" name="password">
<input id="submit" type="submit" value="Sign up" name="submit">
</form>

你做错了。

试试这个

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

该值必须是编码点火器为 CSRF 令牌计算的值。

或使用表单助手,代码点火器将自动添加此隐藏字段。

就我而言,我只是增加了"csrf_expire"变量 - 令牌应该过期的秒数。

从 $config['csrf_expire'] = 7200; 自 $config['csrf_expire'] = 28800;

change $config['csrf_regenerate'] = TRUE;

$config['csrf_regenerate'] = FALSE;在配置文件中

如果你只是想完全摆脱错误......
绕过它们的最简单解决方案是:

  1. 打开/config/config.php 文件

  2. 找到以下行:
    $config['csrf_protection'] = TRUE;

  3. 将其替换为...
    $config['csrf_protection'] = FALSE;

  4. 保存更改。


注意:关闭 CSRF 保护意味着您将面临 CSRF 攻击。