在php项目中,我生成了一个csrf
token ,将其输入 session
后来将它与$_POST['token']
进行了比较要求。现在我需要 github.pages 的这个功能。我找到了如何使用 JS 做同样的事情。但是我现在应该如何以及用什么来比较 php 配置中的这个参数?
<form class="" action="https://somewhere.com/form/form.php" method="POST" id="contact_form">
<input type="text" name="name" value="">
<input type="email" name="email" value="">
<input type="hidden" name="token" value="">
<a id="submitBtn" href="#"></a>
</form>
if($_POST['token']==?????){
//code
}
是否有 bin2hex(random_bytes(32))
的 php 类似物?纯JS的功能?
最佳答案
虽然这在技术上是可行的,但它并不安全,因为 csrf token 的意义在于您可以证明表单是由请求它的同一客户提交的。因此,服务器为客户端提供了一个 secret 值,该值也在 session 中加密存储。如果客户端发送了正确的值,则证明他们也请求了该页面。如果客户端(使用 JavaScript)生成 token ,则无法证明任何内容。
关于javascript - 是否可以使用 js 生成 csrf token 并使用 php 验证它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66513113/