4

I want to delete an image when a user leaves my site or he closes the browser or he enters any other url in the address bar.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
falakistan
  • 57
  • 3

4 Answers4

2

You cannot capture when a user chooses to leave a website. You would be best to have a log somewhere of when they last hit your website from a session, and have a cron job of some kind to clean out their image after some period of time.

Carl Owens
  • 1,292
  • 8
  • 14
0

This is difficult to implement. There is no right way to detect when users leave your site. The most accurate method is to add a script to your pages that requests some URL periodically with a small interval. When user leaves, this URL stops being requested and you realize that user is gone and you can delete some stuff.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
0

Well I am not such an good JavaScript Pro, but i think there might be a function to check if the user is leaving the page or not. ( Think to this popup which comes from time to time, where you get asked if you really want to leave this page. ) You check now if this event ( page leaving ) is occuring and when it does, you send an AJAX request. Your PHP responses with the deletion of your image.

Like i said, not sure if this function in Javascript is working or not.

M. Hirn
  • 725
  • 7
  • 22
0

You can use Javascript, on the onbeforeunload event, then execute an AJAX request with async set to off, so it "blocks" the browser before the user leaves

You can use jQuery to do the async AJAX call, like this:

window.onbeforeunload = function(){
   $.ajax({
      url:"your_delete_script.php", 
      async:false
   });
}

or you may create a image on-the-fly that acts like a beacon to execute an script.

window.onbeforeunload = function(){
  var img = new Image();
  img.src = "script.php";
}

just make sure your script in that part got ignore_user_abort so it will execute independently if the user closed the browser or not

pocesar
  • 6,860
  • 6
  • 56
  • 88
  • 1
    You should not 100% rely on `onunload` and `onbeforeunload`. See: http://stackoverflow.com/questions/3876827/window-onunload-only-fires-when-a-tab-is-closed-in-firefox-not-the-entire-brows and http://stackoverflow.com/questions/1594548/dom-window-unload-event-is-it-reliable for instance – nico Dec 01 '12 at 10:46
  • I know, but he has no other option. It's his best bet, _unfortunately_ – pocesar Dec 01 '12 at 10:54
  • 1
    No, his best hit would be to implement a server-based system, where he checks for expired sessions. – nico Dec 01 '12 at 10:56
  • the user can still be idle in the page and the session expires, then what? – pocesar Dec 01 '12 at 10:58
  • The client browser may not have javascript enabled and so a server side solution would be better. – Carl Owens Dec 01 '12 at 11:00
  • Then you delete the image as the session is expired. Just use a reasonable delay, say half an hour. The user may also be using Opera, and your solution won't work at all there. It is not at all a simple task, I would implement a JS + server side solution to be on the safe side. – nico Dec 01 '12 at 11:00
  • PHP doesn't have a clear way to list all current sessions, unless he keeps the sessions in the database, which is also a bad idea. – pocesar Dec 01 '12 at 11:15