Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Sensing when a user leaves a web site

Posted by enquirer (enquirer), 11 July 2003
Having looked through your site and not found what I'm looking for, I taking your word for it and e-mailing you rather than 'not pulling my hair out'.

My problem is this - I want to create a demo 'build a web page' site using Perl 5. I don't have a problem with any of this - users create a temporary file on entry which will contain HTML code - to display their page.

However, what I want to do is arrange for the server to automatically delete their temporary file when they close their browser window or move to another website. Yes I know that unlink will delete the file - what I don't know is how the server senses that the user has closed the browser window or moved to another URL and what commands are used in Perl to recognise this move and respond by deleting the temporary file.

I suspect that I should be considering a small cgi program that is called when such a move occurs but I have no idea how I can implement this.

Any help would be most appreciated.

Text reproduced with permission of the author

Posted by admin (Graham Ellis), 11 July 2003
Web sites are a bit like Hoggwart's Hall - one moment someone's there and the next, they're gone in a puff of virtual smoke. Problem is - you don't know if they've truely gone or simply become invisible; if you wait a while, and they don't call up pages any more it's fair bet they're not around ....

Or more technically ....

Each time a form is submitted, a new connection is made.  There's no continuinty there.   So the absence of connections for a while is the best that you'll get.

Solution?   Write a bit of code that deletes all temporary files over (say) 8 hours old.   You can run it either
a) Through crontab (but I don't think you have that on your ISP)
or b) At the start of the script that you run to create such temporary files - that way, you have your new users clean up after your old and they don't even realise they're doing it for you!

Here's a subroutine I use for the purpose of cleaning up a directory called sess by deleting all files that haven't been change in a tenth of a day from it .....

sub bookkeeper {
       # Delete out old sessions that have been abandoned
       opendir (DH,"sess");
       while ($file = readdir DH) {
               next if ($file =~ /^\./);
               if (-M "sess/$file" > 0.1) {
                       unlink "sess/$file";

Posted by enquirer (enquirer), 11 July 2003
Many thanks for taking the time and trouble to reply to my question - it is much appreciated. And please feel free to post it to your forum - sometimes it is the answer to niggling 'small' questions like this that is so hard to find and is not usually included in the on-line tutorials that are available.

I did try and give the problem some thought after I'd sent the e-mail to yourself and suddenly realised that I could use the onUnLoad event handler in JavaScript to call a simple cgi script that would delete the file in question if the user either closed the browser window or keyed another URL in the browser's location window (or indeed clicked on a link on the page to take them off-site although the page in question doesn't have such links).

To prevent the script deleting the file if the user was simply refreshing the page, the delete script could use the HTTP_REFERER variable from the Environment Variables to check which page was calling the delete script. If the page URL was that of the page that creates the demo pages I mentioned, the delete function would not be actioned. If it was a different URL, the delete function would be actioned.

This delete cgi script could work invisibly (i.e. not generate any html to display a new page) or pop up a simple page that said 'Thank-you for trying out demo - your temporary page has now been deleted'.

Posted by admin (Graham Ellis), 11 July 2003
Hmm ... seems a lot of work to try and trap all the cases.  I worry a bit about pages being left over time by browsers crashing (it does happen), or people who drop their modem connections while still on your page.   And I think I would prefer a longer timeout in case someone goes away and then uses back to try and get back ....

I'll be very interested to hear how you get on with the JavaScript ...

Posted by John_Moylan (jfp), 11 July 2003
I can't help thinking that even using JavaScript onUnload event you will still have to implement Grahams "delete old files" solution as users have the ability to turn JavaScript off.

Just  a thought.

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2023: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho