Possible Duplicate:
Detecting Browser exit in PHP
I have some user related files in my server and I want them to be deleted when user closes the browser or leaves my page. Is it possible? Is there any way to do it?
Possible Duplicate:
Detecting Browser exit in PHP
I have some user related files in my server and I want them to be deleted when user closes the browser or leaves my page. Is it possible? Is there any way to do it?
Not just with PHP.
PHP runs server-side, and is far done processing your page by the time the user will have a chance to close their browser. You could technically detect if PHP was still processing the page and the user closes it, with a specific configuration. However, it is not ideal. See connection_aborted()
.
What you need to do is set up a long-polling connection with JavaScript, and monitor it server-side. You will then get an idea for when that window is closed. That connection could be made to your PHP script, allowing PHP to check connection_aborted()
. Note that you will need to set up ignore_user_abort()
for this to work, or configure PHP.ini accordingly.
You could use javascript to make an ajax call to a .php file every few seconds.
PHP is *server*side so with only PHP this isn't possible in most cases. A possible solution would be to let a cronjob run everynight and let it delete the old files.