dongshi7433 2014-10-30 18:20
浏览 50
已采纳

睡觉前发送回声不起作用,有什么选择吗?

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);

            });
  • 写回答

1条回答 默认 最新

  • dongzhuonao8429 2014-10-30 18:37
    关注

    You can't send partial responses back to the client; the response will not be sent until the code is finished executing.

    You should be keeping track of the user's session and how long they need to wait before logging in persistently on the server. This way your code can be executed across multiple requests (instead of trying to stream responses back to the client socket-style while causing the php script to sleep—it just won't work).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线