CodeIgniter,Csrf令牌 [英] CodeIgniter , Csrf token

查看:96
本文介绍了CodeIgniter,Csrf令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦

查看

<form method="post" action="test/csrf">
<input type="hidden" name="<?=$this->security->get_csrf_token_name();?>" value="<?=$this->security->get_csrf_hash();?>">

控制器

echo $this->input->post($this->security->get_csrf_token_name());

我无法显示令牌代码

推荐答案

访问控制器中的Csrf令牌

在控制器中,您可以通过以下方式获取csrf的名称和值:

In controller u can get name and value of csrf as follow

        echo $this->security->get_csrf_token_name(); // for the name
        echo $this->security->get_csrf_hash();  // for the value    

在配置文件中启用CSRF的值

 $config['csrf_protection'] = TRUE;
 $config['csrf_expire'] = 7200;
 $config['csrf_regenerate'] = TRUE;

1。通过表单助手使用过的CSRF令牌

我们有两种添加CSRF令牌的方法;如果我们正在考虑使用CodeIgniter表单帮助程序类更新您的表单,则CSRF令牌将自动添加,或者如果您正在考虑以自定义表单进行调整,则需要添加自定义隐藏输入名称及其值。

We have two way to add CSRF tokens; if we are thinking to update your form with CodeIgniter form helper class then CSRF tokens will automatic added or if you are thinking to adjust in custom form then we need to add custom hidden input name and its value.

何时使用表单帮助程序类:

When we will use form helper class:

  <?php echo form_open(base_url( 'user/login' ), array( 'id' => 'login', 'class' => 'login' ));?>
      <input type="text" name="username" />
      <input type="password" name="password" />
      <input type="submit" name="submit" value="Submit" />
  <?php echo form_close();?>

使用表单帮助程序类将自动向表单中添加带有随机标记值的输入,以防止CSRF。

Using form helper class will automatically added input filed into the form with a random token value to prevent CSRF.

2。当我们使用自定义表单时:

我们需要添加输入文件,以防止使用CSRF生成自定义表单。

We need to add a input filed to prevent our custom form with CSRF.

    $csrf = array(
    'name' => $this->security->get_csrf_token_name(),
    'hash' => $this->security->get_csrf_hash()
    );

     <input type="hidden" name="<?=$csrf['name'];?>" value="<?=$csrf['hash'];?>" />

如果您使用表单助手,则form_open()会自动在表单中插入一个隐藏的csrf字段。否则,

If you use the form helper, then form_open() will automatically insert a hidden csrf field in your forms. If not,

然后,您可以使用 get_csrf_token_name() get_csrf_hash()

http://www.codeigniter.com/user_guide/libraries/security.html

http://www.sks.com.np/secure-your-codeigniter-application-using-csrf-token /

这篇关于CodeIgniter,Csrf令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆