давно когда то делал себе.
так вот
<?php
function token($type=true){
$arr = array('a','b','c','d','e','f',
'g','h','i','j','k','l',
'm','n','o','p','r','s',
't','u','v','x','y','z',
'A','B','C','D','E','F',
'G','H','I','J','K','L',
'M','N','O','P','R','S',
'T','U','V','X','Y','Z',
'1','2','3','4','5','6',
'7','8','9','0','.',',',
'(',')','[',']','!','?',
'&','^','%','@','*','$',
'<','>','/','|','+','-',
'{','}','`','~');
if($type==true){
$pass = "";
for($i = 0; $i < 12; $i++){
$index = rand(0, count($arr) - 1);
$pass .= $arr[$index];
}
$_SESSION['token'] = $pass;
return $pass;
}else{
if(isset($_SESSION['token'])){
return $_SESSION['token'];
}else{
return false;
}
}
}
function token_input(){
return '<input type="hidden" name="token" value="'.token(true).'" />';
}
function token_check(){
if(token(false)!=false AND $_POST['token']==token(false)){
unset($_SESSION['token']);
return true;
}else{
unset($_SESSION['token']);
return false;
}
}