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 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口