Created
June 17, 2014 22:23
-
-
Save xombra/fd8cf15cba27083b4a10 to your computer and use it in GitHub Desktop.
Crear y verificar Token para evitar csrf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function CREAR_TOKEN($TokenForm) | |
{ $token = md5(uniqid(microtime(), true)); | |
$token_time = time(); | |
$_SESSION['csrf'][$TokenForm.'_token'] = array('token'=>$token, 'time'=>$token_time); | |
return $token; | |
} | |
function VERIFICA_TOKEN($TokenForm, $token) | |
{ if(!isset($_SESSION['csrf'][$TokenForm.'_token'])) { | |
return false; | |
} | |
if ($_SESSION['csrf'][$TokenForm.'_token']['token'] !== $token) { | |
return false; | |
} | |
return true; | |
} | |
# En el formulario Colocar | |
<input type="hidden" name="auth_token" value="<?php echo CREAR_TOKEN('Tok_X'); ?>" /> | |
# y verificar la llegada | |
$token = $_POST['auth_token']; | |
if(!VERIFICA_TOKEN('SeP', $token)){ | |
echo '<div class="alert alert-danger"><p>'.$token.' El intento de acceso no es valido y/o expiro, <br /> Refresque e intente de nuevo</p></div>'; | |
die(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
enviar codigo token