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

时间:2014-10-30 18:20:58

标签: php ajax echo sleep

我很担心该怎么做。我想发送一个回声,AJAX将读取并做出反应。但问题是php脚本在它回响之前完全执行。我试过冲洗和SOF上发现的所有其他方法,但它仍然无法正常工作。我认为这是因为我的免费主机服务器是000webhost - 它们可能有一些与睡眠功能冲突的东西。

有没有办法延迟执行PHP脚本,但在完成之前向客户端发送一个echo(警告用户),然后在禁用提交按钮并显示时间延迟时执行脚本?

请原谅我对AJAX的新手,我最近开始学习它。

  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 个答案:

答案 0 :(得分:0)

您无法将部分回复发送回客户端;在代码完成执行之前,不会发送响应。

您应该跟踪用户的会话以及他们需要等待多长时间才能在服务器上持久登录。通过这种方式,您的代码可以跨多个请求执行(而不是尝试将响应流回客户端套接字样式,同时导致php脚本进入睡眠状态 - 它只是无法工作)。