我正在使用CodeIgniter脚本,该脚本具有以下内容,以阐明这是模块中登录表单的完整代码
<!-- modal login -->
<div id="login" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog white-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<p id="myModalLabel"><i class="icon icon-user"></i> Login </p>
</div><!-- modal-header -->
<div class="modal-body">
<?php if (isset($login_message)) {
echo $login_message;
}
?>
<form method="post" action="/users/login" class="form" id="login-form">
<input type="text" name="uname" placeholder="username" class="form-control" /><br/>
<input type="password" name="upwd" placeholder="****" class="form-control" /><br/>
<input type="submit" name="sbLogin" value="<?=_('Login')?>" class="btn btn-black"/>
<a href="/home/lostpassword" class="btn btn-default">Lost Password</a><br />
<br /><div class="fb-login-button" data-max-rows="1" data-size="small" data-button-type="login_with" data-show-faces="false" data-auto-logout-link="true" data-use-continue-as="false"></div>
<br /><br />Don't have an Account? <a href="/?signup=yes">Create one</a>
</form>
<br />
<div id="login_output_div"></div>
</div>
<div class="modal-footer">
<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div><!-- .modal dialog -->
为了保护表格,我可以添加
<input type="hidden" name="<?=$csrf['name'];?>" value="<?=$csrf['hash'];?>" />
并使用
$csrf = array(
'name' => $this->security->get_csrf_token_name(),
'hash' => $this->security->get_csrf_hash()
);
在配置中启用CSRF后,页面标题中的?这样做会搞乱我的其他表格吗?我需要将它添加到每个表格中吗?
<input type="hidden" name="<?=$csrf['name'];?>" value="<?=$csrf['hash'];?>" />
谢谢。
答案 0 :(得分:0)
您需要做的是打开此文件:
application/config/config.php
然后将CSRF设置为true
$config['csrf_protection'] = true;
然后转到该文件
application/config/autoload.php
将form
添加到名为helper
的数组项中
$autoload['helper'] = array('form');
然后,您应该在codeigniter中使用表单帮助器,您可以在此链接中阅读它:
它确实添加了csrf并为您验证了它。
然后在您的视图上,您可以简单地使用:
<?php $attributes = array('id' => 'login-form');
echo form_open('/users/login', $attributes); ?>
它将自动创建<form>
标记并向其添加csrf,然后,提交此表单后,codeigniter将验证csrf,如果csrf成功,则代码将恢复,否则,它将恢复将失败并显示错误消息。