tatty27
tatty27

Reputation: 1554

Run command as page is being closed?

Is there a way that I can run a php command if a window is closed?

For example, I have a script that the user uses to upload files to a temp directory on the server ready for the user to decide if they want to keep them or not. However, as it stands at the moment, if the user just leaves the page the files stay there.

Thanks

Upvotes: 0

Views: 101

Answers (3)

Zak
Zak

Reputation: 7515

PHP scripts have to be initialized by "something". With that said:

You may consider a CRON job that runs every so often and runs a PHP script for you. Lets say for example you wanted to 'clear' the temp directory every half hour, a CRON job would work perfectly for that. -- The reason JavaScript might not be your best bet here is, like you said, if the user closes the window, JavaScript ceases to function.

Upvotes: 0

tdedecko
tdedecko

Reputation: 1482

You might be able to intercept the page exit event in javascript and pass that to your php application: Intercept page exit event

This seems like it would be better implemented as a cron job on the server that would cleanup files in a temp directory.

Upvotes: 0

thaJeztah
thaJeztah

Reputation: 28987

You may add a javascript that warns the user before leaving the page using onBeforeUnload, some examples can be found here:

JavaScript + onbeforeunload

Upvotes: 1

Related Questions