How to delete an image when a user leaves my site in php?

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

+4
source share
4 answers

You cannot capture when a user wants to leave the site. It’s best to have a log somewhere the last time they get to your site from a session and have some kind of cron job to clear their image after some time.

+2
source

It is hard to implement. Unable to determine when users leave your site. The most accurate method is to add a script to your pages that periodically request a certain URL with a small interval. When the user leaves, this URL is no longer requested, and you understand that the user has left, and you can delete some things.

0
source

Well, I'm not such a good JavaScript Pro, but I think there might be a function to check if the user leaves the page or not. (Think of this pop-up that comes from time to time where you are asked if you really want to leave this page.) You check to see if this event occurs (page deletion), and when that happens, you send an AJAX request. Your PHP answers with deleting your image.

As I said, I'm not sure if this function works in Javascript or not.

0
source

You can use Javascript in the onbeforeunload event and then execute an AJAX request with the asynchronous disabled, so it "blocks" the browser before the user leaves

You can use jQuery to call async AJAX, for example:

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

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

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

just make sure your script receives ignore_user_abort in this part, so it will be executed independently if the user closed the browser or didn’t execute

0
source

All Articles