PHP有像Rails这样的真实性令牌吗?

Does PHP have an authenticity token like Rails?

PHP有自己版本的Rails真实性令牌吗?

1
2
<meta name="csrf-token" content="<%= form_authenticity_token %>" />
<meta name="csrf-param" content="authenticity_token" />

如果不是,实现相同功能的最佳方法是什么?


输出到窗体时:

1
2
3
4
$token = md5(time() . rand(1,100));
$_SESSION['token'] = $token;

<input type='hidden' name='token' value='<?=$token;?>'/>

发布后:

1
2
3
4
if(empty($_POST['token']) || $_POST['token'] !== $_SESSION['token']){
  exit("Bad token!");
}
unset($_SESSION['token']);