1 | <?php |
2 | |
3 | function creaToken(string $pagina, int $duracionEnMinutos) |
4 | { |
5 | $criptografiaFuerte = true; |
6 | |
7 | |
8 | $token = [ |
9 | "expiracion" => time() + 60 * $duracionEnMinutos, |
10 | |
11 | "texto" => bin2hex(openssl_random_pseudo_bytes(80, $criptografiaFuerte)) |
12 | ]; |
13 | |
14 | |
15 | if (isset($_SESSION[$pagina])) { |
16 | |
17 | $tokensParaPagina = $_SESSION[$pagina]; |
18 | |
19 | |
20 | foreach ($tokensParaPagina as $llave => $tokenParaPagina) { |
21 | if ($tokenParaPagina["expiracion"] > time()) { |
22 | unset($tokensParaPagina[$llave]); |
23 | } |
24 | } |
25 | |
26 | |
27 | $tokensParaPagina[] = $token; |
28 | $_SESSION[$pagina] = $tokensParaPagina; |
29 | } else { |
30 | |
31 | |
32 | $_SESSION[$pagina] = [$token]; |
33 | } |
34 | |
35 | return $token["texto"]; |
36 | } |
37 | |