I want to do a php counter that eliminates the session $_SESSION["error"]
if a time has passed N. $_SESSION["error"]
is accumulating the counts, 1, 2, etc.
The problem is that the first time I see the echo
returned the alert goes blank. The second time I enter I see 1, 2, etc.
I do not understand what this is doing wrong.
$ahora = @date("Y-n-j H:i:s");
$count=$_SESSION["error"];
$_SESSION["error"]=$count+1;
$timeOutError = 10; //Tiempo en segundos
$fechaGuardadaError = $_SESSION["app"]["ultimoAccesoError"];
$tiempoError = (@strtotime($ahora) - @strtotime($fechaGuardadaError));
if ($tiempoError >= $timeOutError){
unset($_SESSION["error"]);
}
$_SESSION["app"]["ultimoAccesoError"]=$ahora;
echo $_SESSION["error"];
This is the function from which I call the php file, when it leaves the file it returns to the function and it is where I check the value of echo
.
function login(){
usuario=$('#idUsuario').val();
clave=$('#idClave').val();
var direccion = 'principal.html';
var parametros = {
"usuario":usuario,
"clave":clave,
}
$.ajax ({
data: parametros,
url: "php/login.php",
type: "POST",
success: function(data){
alert(data); //Aqui veo el echo de php
if(data==-1){ // Correcto
location.href=direccion;
}
else if(data>=0&&data<=2){ //Incorrecto
intentos= 3-data;
$('#idTextoError').html("<center><strong>Identificación erronea, quedan " +intentos+" Intentos.</strong></center>"); //Error de Usuario/Clave
$('#idTextoError').show();
}
else if(data>=3){
$('#idTextoError').html("<center><strong>Ha superado el numero maximo de intentos. Espere 1 minuto hasta pronximo intento</strong></center>"); //Error de Usuario/Clave
$('#idTextoError').show();
}
}
});
}
The way in which I have solved it is the following:
$ahora = @date("Y-n-j H:i:s");
if($_SESSION["error"]==0){
$fechaGuardadaError=$ahora;
}
else{
$fechaGuardadaError = $_SESSION["app"]["ultimoAccesoError"];
}
$_SESSION["error"]=$_SESSION["error"] +1;
$timeOutError =6; //Tiempo en segundos
$tiempoError = (@strtotime($ahora) - @strtotime($fechaGuardadaError));
if ($tiempoError > $timeOutError){
$_SESSION["error"]=0;
}
$_SESSION["app"]["ultimoAccesoError"]=$ahora;
openlog("areaPrivada", LOG_PID | LOG_PERROR, LOG_LOCAL0);
syslog(LOG_WARNING, "Login error: Usuario: $usuario, IP: {$_SERVER['REMOTE_ADDR']} ");
closelog();
echo $_SESSION["error"];
New Function Login:
usuario=$('#idUsuario').val();
clave=$('#idClave').val();
var direccion = 'principal.html';
var parametros = {
"usuario":usuario,
"clave":clave,
}
$.ajax ({
data: parametros,
url: "php/login.php",
type: "POST",
success: function(data){
if(data==-1){ // Correcto
location.href=direccion;
}
else if(data>=0&&data<5){ //Incorrecto
intentos= 5-data;
$('#idTextoError').html("<center><strong>Identificación erronea, quedan " +intentos+" Intentos.</strong></center>"); //Error de Usuario/Clave
$('#idTextoError').show();
}
else if(data>=5){
$('#idTextoError').html("<center><strong>Ha superado el numero maximo de intentos. Espere 5 minutos hasta pronximo intento</strong></center>"); //Error de Usuario/Clave
$('#idTextoError').show();
}
}
});
}