Sensing when a user leaves a web site
Posted by enquirer (enquirer), 11 July 2003Having 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 2003Web 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 .....
Posted by enquirer (enquirer), 11 July 2003Many 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.
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 2003Hmm ... 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 ....
Just a thought.
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: email@example.com • WEB: http://www.wellho.net • SKYPE: wellho