I'm confused about what to do. I want to send an echo, which the AJAX will read and react to. But the problem is the php script is executed completely before it echoes. I tried flushing and all other methods found on SOF but it still didn't work. I think it's because my free host server is 000webhost - they may have something that conflicts with the sleep function.
Is there a way to delay an execution of the PHP script, but before completion send an echo to the client (which alerts the user), and afterwards the script will execute while the submit button is disabled and time delay is displayed?
Please excuse my noviceness on AJAX, I've recently started learning it.
if ($failed_attempts_ip_user >= $first_throttle_limit) {
foreach ($throttle as $attempts => $delay) {
if ($failed_attempts_ip_user > $attempts) {
if (is_numeric($delay)) {
$next_login_minimum_time = $latest_attempt + $delay;
if (time() < $next_login_minimum_time) {
$remaining_delay = $next_login_minimum_time - time();
echo "You must wait " . $remaining_delay . " seconds before your next login attempt.";
sleep($remaining_delay);
} else {
return "safe to login";
}
} else {
if ($delay === "reCaptcha") {
echo 'reCaptcha';
} else {
//$max_attempt = array_search('temp_blocked', $throttle); // search key 'temp_blocked' for its index.
echo "Too many login attempts detected. Please wait " . $temp_block_attempts_limit . " minutes before your next attempt.";
}
}
break;
}
} // foreach close tag
echo "Email/username and/or password is incorrect.";
exit;
}
echo "user_password_does_not_match";
}
AJAX:
$(function ajaxLogin() {
$("#login_form").submit(function(e) {
e.preventDefault();
$("input[name='submit']").val("Processing...");
$("input[name='submit']").addClass("disableButton");
$("input[name='submit']").prop("disabled", true);
var receiveDataLogin = $.ajax({
dataType: "text",
type: "POST",
url: 'includes/login_script.php',
timeout: 5000,
data: $(this).serialize()
});
receiveDataLogin.done(function(data){
if(data.trim() == "empty_email_username_detected") {
alert("Please enter in your email address.");
} else if(data.trim() == "empty_password_detected") {
alert("Please enter in your password.");
} else if(data.trim() == "temp_block_detected") {
alert("Too many login attempts. Please try again in 15 minutes.");
} else if(data.trim() == "successfully_logged_in") {
$('.form_menu_wrapper').slideUp(200);
alert("You have signed in successfully!");
} else if(data.trim() == "Email/username and/or password is incorrect.") {
alert("Your email address/username and/or password is incorrect.");
} else if(data.trim() == "user_password_does_not_match") {
alert("Your email address/username and/or password is incorrect.");
} else if(data.trim() == "You must wait 1 seconds before your next login attempt.Email/username and/or password is incorrect.") {
alert("You must wait 1 second before your next login attempt.");
} else if(data.trim() == "You must wait 2 seconds before your next login attempt.Email/username and/or password is incorrect.") { alert("You must wait 2 seconds before your next login attempt.");
$("input[name='submit']").val("Sign in");
} else if(data.trim() == "You must wait 4 seconds before your next login attempt.Email/username and/or password is incorrect.") {
alert("You must wait 4 seconds before your next login attempt.");
} else if(data.trim() == "You must wait 8 seconds before your next login attempt.Email/username and/or password is incorrect.") {
alert("You must wait 8 seconds before your next login attempt.");
} else if(data.trim() == "You must wait 16 seconds before your next login attempt.Email/username and/or password is incorrect.") {
alert("You must wait 16 seconds before your next login attempt.");
} else if(data.trim() == "You must wait 20 seconds before your next login attempt.Email/username and/or password is incorrect.") {
alert("You must wait 20 seconds before your next login attempt.");
} else if(data.trim() == "reCaptchaEmail/username and/or password is incorrect.") {
alert("reCaptcha -- will add reCaptcha later");
} else if(data.trim() == "Too many login attempts detected. Please wait 15 minutes before your next attempt.Email/username and/or password is incorrect.") {
alert("Too many failed login attempts detected. Please wait 15 minutes before your next attempt.");
} else {
alert("Error. Please try again later.");
}
$("input[name='submit']").val("Sign in");
$("input[name='submit']").removeClass("disableButton");
$("input[name='submit']").prop("disabled", false);
});