Forum Moderators: coopster & phranque

Message Too Old, No Replies

Checking the browser is still connected....

perl, browser, connected, check

         

KeithBoynton

10:47 am on May 30, 2004 (gmt 0)

10+ Year Member



Hi everyone,

I have a CGI script which performs a fair bit of work and I want to stop it running if the user closes their browser, hits back etc.

It's pretty easy in asp, as I could use response.isclientconnected

Take a look at the following code:


print "Content-Type: text/html\n\n";

while ($recordExists) {

# HERE I WOULD LIKE TO CHECK IF THE CLIENT IS CONNECTED OTHERWISE TERMINATE SCRIPT

my $recordData=getRecordData();
print "Data: ${recordData}<br>";

$recordExists=isThereAnother();
}

print "Done!";

exit;

Is it possible? Any help would be greatly appreciated....

MichaelBluejay

6:37 pm on May 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm pretty sure you can't do this in Perl, since I don't think Perl has any knowledge of what's going on in the browser. I can think of a way that would do it with JavaScript:

<BODY onunload="window.open('stopprocess.html')">

And <stopprocess.html> has an SSI call to the Perl script:

<HTML><BODY>
<!--#include "stopprocess.cgi">
<SCRIPT language=JavaScript>
window.close();
</SCRIPT>
</BODY></HTML>

But popping up windows is pretty rude, even if you close them right away.

This is a very interesting question, though. I'm interested if other users know of a better mousetrap for this one.

KeithBoynton

12:33 pm on May 31, 2004 (gmt 0)

10+ Year Member



I have actually found a way to do this but there is a downside. It only works with PerlScript so is only available to Win32 users unfortunately.

The code below works fine:


<% @LANGUAGE="PerlScript" %>
<%
require 'loggingLibrary.pl';

for(my $i=0;$i<100;$i++) {
if ($Response->{IsClientConnected}) {
# Do whatever
} else {
writeToLog("Script terminated at step: $i");
exit;
}
}
%>

Depending on how fast the loop executes you may want to "mod" the isClientConnected test so it doesn't test on every pass.

SeanW

12:48 pm on May 31, 2004 (gmt 0)

10+ Year Member



I'm wondering if you can check the status of the STDOUT file descriptor... If the browser closes the connection does the web server close it's connection with the CGI?

Sean

VectorJ

3:53 pm on Jun 1, 2004 (gmt 0)

10+ Year Member



If you're using Unix/Apache, I'm pretty sure that the webserver terminates the CGI by sending it a SIGINT when the browser goes away, so in that case this problem is already taken care of.