2

Why is the last line of this function required to make this work? I don't understand what's going on here. It does work though.

function close_connection() {
    // if we're using sessions this stops them from hanging on other requests
    session_write_close();
    // get the content length from the buffer (the whole point of the buffer)
    header('Content-Length: 0');
    // close the connection
    header('Connection: close');
    // flush the buffers to the screen
    flush();

    // connection closing doesn't work without this. I don't know why
    echo ' ';
}

1 Answer 1

1

Headers are sent whenever the first byte is output to the browser.
You could also try to use ob_end_flush();

Sign up to request clarification or add additional context in comments.

5 Comments

I tried with ob_end_flush() before actually. it wasn't doing anything so I removed it. Probably because my buffer was totally empty?
you could check that by using : $obsize = ob_get_length(); and then use that output size in your header Content-Length: header("Content-Length: $obsize");
Can I flush buffers without sending any content?
Yes, by using ob_clean() or ob_get_clean()
If your server compresses output, you need to disable it with header("Content-Encoding: none\r\n");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.