Tengo el código para iniciar el token:
session_start();
$token_string = bin2hex(random_bytes(32));
$_SESSION['token_'.$token_string] = time();
$token = $_SESSION['token_'.$token_string] = time();
El código para poner un escondido input en mi formulario
<form ... >
<?php
echo '<input type="hidden" name="token" value="'.$token.'" />';
?>
...
</form>
Y el código para ver si el token ha sido recibido correctamente:
$max_minutos_csrf = 60;
$token_string = $_POST['token'];
if ( !isset($_SESSION['token_'.$token_string]) ||
$_SESSION['token_'.$token_string] < time() - $max_minutos_csrf * 1000 ) {
header('HTTP/1.0 400 Bad Request');
exit('Error de CSRF.');
} else {
// Ejecutar el resto de Php Mysql
}
Me devuelve este error:
Notice: Undefined index: token_1491320450
Si hago echo $_POST['token']
me da esto: 1491320450
Si hago var_dump($_SESSION)
me da:
array (size=7)
'user_session' => string '9' (length=1)
'token_f31339250d26b0e4af79749a6ecc56f2e7e1777b41d62b7472174b10443426fc' => int 1491322443
'token_7c65f80f5fb3d47d00b4480580edcbc7a60694018309f50f413bad16ed2634af' => int 1491322444
'token_87f9a9bfb2397f5c4b5b0865e3fcc513163a446bcb6cd47d8ac112e66f813b8b' => int 1491322445
'token_8ca703ee6144b88fc7df80ac46a669d109636f1a94acddb1a3db43a6c841382d' => int 1491322446
'token_082860bf9f77bfb159e97b0a8e17bfb71bf6bfc804e2bdf1572b45991424c0e3' => int 1491322699
'token_e4999ec00c11f9311f0778c38bfbfe8887a82907e386830d747af17be0b88ee2' => int 1491322700
sessions.php
y esta incluida en el archivo donde recibo elPOST
– Lukas el 4 abr. 17 a las 15:46