haku Posted November 18, 2009 Share Posted November 18, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/181974-close-http-connection/ Share on other sites More sharing options...
oni-kun Posted November 18, 2009 Share Posted November 18, 2009 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.. Quote Link to comment https://forums.phpfreaks.com/topic/181974-close-http-connection/#findComment-959837 Share on other sites More sharing options...
haku Posted November 18, 2009 Author Share Posted November 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/181974-close-http-connection/#findComment-959841 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.