Ho creato una classe 'Sicurezza' per la creazione di un token:
Codice PHP:
class Sicurezza {
public function cript ($tipo = null) {
switch($tipo) {
case 'salt':
$salt = mcrypt_create_iv(64, MCRYPT_DEV_URANDOM);
$salt = base64_encode($salt);
$salt = "$2a$09$".substr($salt,0,22).'$'; //algortimo Blowfish return $this->salt;
break;
case 'token':
$token = md5(uniqid(microtime(), true));
return $token;
break;
default:
$token = md5(uniqid(microtime(), true));
return $token; } }}
poi una pagina test per provarla:
Codice PHP:
session_start();
include('class_sicurezza.php');
$token = Sicurezza::cript('token');
$_SESSION['token'] = $token;
if(isset($_POST['entra'])) {
if($_SESSION['token'] == $_POST['token']) {
echo "OK!";
}else {
echo "Errore token";
}
}
codice HTML:
<!DOCTYPE html><html>
<head>
</head>
<body>
<form method="POST" action="test.php">
<input type="hidden" name="token" value="<?php echo $token; ?>" >
<input type ="text" name="utente"><br>
<input type="password" name="password"><br>
<input type="submit" name="entra"><br>
</form>
</body>
</html>
La sessione token ed il valore del post token non sono mai eguali. Se cerco di risalire al valore di $_POST['token'] si genera l'errore: Undefined index: token.
Vi chiedo perciò aiuto