Jump to content

Close HTTP connection


haku

Recommended Posts

I am building an application that works with a smartcard reader. The user scans their smartcard, and then the smartcard reader access a script on my server (at a particular URL). The reader then waits for a simple two-character response (OK or NG), and gives off a sound indicating whether the scanning was successful or not.

 

However, the script on the server does much more than just send out those two characters - it logs the user in or out of the system, adds points to their account, and does many other things in the background.

 

The problem I am having is that the smartcard reader waits until the HTTP connection has closed before it plays the sound indicating success or failure. Now, the two characters that my script sends out are sent back very early in the process - upon benchmarking there is less than one second of processing time between the script receiving the data and sending the response. However the whole script overall takes about 5-10 seconds, during which time the HTTP request is open, and the card reader is waiting for it to close.

 

Does anybody know how I can close the HTTP connection, but still let my script run in the background?

Link to comment
Share on other sites

I believe you would need to use the header such as: Connection: Keep-alive. If you send a Content-Length header with your PHP script, Apache will be able to keep the connection open and maintain keep-alive without appending ": close" to the end thus breaking it..

Link to comment
Share on other sites

Gracias!

 

Actually, in one of my moments of being lame, I only googled for about 8 seconds before starting this thread. A little deeper digging gave me this:

 

ob_end_clean();
header('Connection: close');
ob_start();
echo 'OK';
$size = ob_get_length();
header('Content-Length: ' .$size);
ob_end_flush(); // Strange behaviour, will not work
flush();            // Unless both are called !
// Execute remaining code here

 

Works perfectly. I get the response in about 1.5 seconds now.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.