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....
<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.
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.