PHP CSRF 表单令牌 +验证建议
我有一个提交表单,有 9 个字段,其中 6 个需要验证,包括一个带有文件大小和文件类型验证的上传字段。
生成随机令牌,以防止 CSRF 起作用,但是使用令牌时验证的正确方法是什么?
如果我在同一文件中进行验证,则会通过验证重新加载重新生成令牌。 (可以防止这种情况吗?我已尝试 isset()
但仍然会重新生成。)但是,使用同一文件会阻止将用户名称和电子邮件存储在会话中。
最好在单独的文件中进行验证,然后重定向回表单,并在每个错误的 URL 中包含基本变量,即 http://www.example.com/form?n=1
还意味着将表单数据存储在会话中,因此如果重定向上存在错误,可以重新填充表单。
非常感谢任何帮助。
I've got a submission form, with 9 fields, 6 of which require validation, including a upload field with file size and file type validation.
Generating a random token, to prevent CSRF is working, but what is the correct way to validate when using a token?
If I do the validation within the same file, the token is regenerated with the validation reload. (can this be prevented? I've tried isset()
but still regenerates.) However using the same file prevents the users Name and Email from being stored in a session.
Is it best to do the validation within a separate file, which then redirects back to the form with basic variables in the URL for each error, i.e. http://www.example.com/form?n=1
Using a separate file would also mean storing the form data within session, so the form can be repopulated if errors exist on the redirect.
Any help gratefully received.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据经验,CodeIgntier 在 CSRF 方面表现出色实施,以及其他安全措施。我建议您仔细阅读他们的代码,以更好地理解整个过程。另请参阅此。
From experience, CodeIgntier does great CSRF implementation, among other security mesures. I would suggest that you go over their code to gain a good understanding of the whole process. Also see this.