Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8409629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:57:49+00:00 2026-06-09T23:57:49+00:00

EDITED I’ve got an ajax call (using $.ajax() ) which calls the following php

  • 0

EDITED

I’ve got an ajax call (using $.ajax()) which calls the following php script.

for ($i=0;$i<40;$i++) {
    echo " ";
    flush();
    if (connection_aborted()) {
        log_message('error','CONNECTION IS ABORTED!!!!!');
        exit;
    }
    else {
        log_message('error','connection not aborted :(');
    }
    sleep(1);
}

This goes for 40 Seconds.

If I close the browser window which triggered the call, connection_aborted() still returns false, even if I sent explicitly a string and flushed the buffer!

Does anyone have an answer here please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:57:51+00:00Added an answer on June 9, 2026 at 11:57 pm

    You will need to add “ignore_user_abort(true);” on top of the PHP script, and to call “ob_flush()” after echoing something from script (For why see PHP flush() man page). Working example (proof of concept):

    <?php
    
    ignore_user_abort(true);
    
    function log_message($s, $ss) {
      $myFile = "log.txt";
      $fh = fopen($myFile, 'a') or die("can't open file");
      $stringData = $s . ": " . $ss . "\n";
      fwrite($fh, $stringData);
      fclose($fh);
    }
    
    
    
    for ($i=0;$i<5;$i++) {
    
        echo "<br>";
        //flush();
        ob_flush();
    
        if (connection_aborted()) {
            log_message('error1', connection_status());
            exit;
        }
        else {
            log_message('error2', connection_status());
        }
    
        sleep(1);
    }
    

    P.S. connection_status() returns 0 if connection is still active, and in case of closed one returns 1.

    EDIT:

    My bad. Call both flush() and ob_flush() (please read flush() man page, link above, and answers from this topic), or otherwise might not work, depending on server/php configuration.
    The following code was tested on WAMP with PHP 5.3.8 (works without calling flush()), and now on Ubuntu with PHP 5.3.10. where flush() call before ob_flush() is necessary.

    Full code for testing:

    index.html:

     <html>
      <head>
        <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
    
        <script>
    
          $(document).ready(function() {
    
            $.ajax({
              url: "script.php",
              context: document.body
            }).done(function(data) { 
             alert(data);
            });
    
          })
    
        </script>
    
      </head>
    
      <body>
      </body>
    
    </html>
    

    script.php:

    ignore_user_abort(true);
    
    function log_message($type, $message, $file = 'log.txt') {
        $fh = fopen($file, 'a') or die("can't open file");
    
        $conn_status = connection_status();
    
        if($conn_status === CONNECTION_NORMAL) {
            $status = 'normal';
        } elseif($conn_status  === CONNECTION_ABORTED) {
             $status = 'aborted';
        } else {
            $status = 'timeout';
        }
    
        $aborted = connection_aborted() ? 'yes' : 'no';
    
        $data  = $type . ': ' . $message . "\n";
        $data .= 'Connection status: ' . $status . "\n";
        $data .= 'Aborted: ' . $aborted . "\n\n\n";
    
        fwrite($fh, $data);
        fclose($fh);
    }
    
    
    
    for ($i = 0; $i < 10; $i++) {
    
        echo "<br>";
        flush();
        ob_flush();
    
        if (connection_aborted()) {
            log_message('Error', 'Connection closed by user!');
            exit;
        }
        else {
            log_message('Info', 'Everything is fine. Move along...');
        }
    
        sleep(1);
    }
    

    After you call index.html page, and close tab or whole browser you should see in log.txt file next info:

    Info: Everything is fine. Move along...
    Connection status: normal
    Aborted: no
    
    
    Info: Everything is fine. Move along...
    Connection status: normal
    Aborted: no
    
    
    Info: Everything is fine. Move along...
    Connection status: normal
    Aborted: no
    
    
    Error: Connection closed by user!
    Connection status: aborted
    Aborted: yes
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDITED I'm using Twitter Bootstrap and need to move navbar right( not float:right, but
Edited Question: This should be clear. using System; namespace UpdateDateTimeFields { class Program {
(Edited a lot) I've got some classes with Abstracts Members. The concrete type of
edited: This is what i need: sendpost = function(a,b,c){ return jQuery.post('inc/operations.php', {a:b}, c, json);
EDITED I m using this select query with left join: $updaterbk = SELECT j1.
Edited : got it worked. Generic Collections to be used as a parameter of
EDITED * * Hello I am new to PHP and trying to figure out
EDITED I'm trying to use jquery/ajax to display data returned from a django method.
EDITED to show real example How can I call a generic function from a
[edited] I am trying to make a script that downloads a file, the problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.