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

时间:2011-02-24 21:58:05

标签: php ruby-on-rails-3 authentication token

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

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

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

1 个答案:

答案 0 :(得分:8)

输出到表格时:

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

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

POST后:

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