dongshi7433 2014-10-30 10: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 10: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).

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部