Execute file in server after window is closed
Hi all,
Iam trying to tigger some file which will be working in backend once the window is closed in php. Iam using onunload function in javascript but it triggers the file once we leave that particular page only. If we closed the window it didnt do anything. Anybody have idea how to execute file in server once the window was closed. Is there any possibilites?
Thanks in advance...
mahe4us
Junior Poster in Training
55 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
may i ask why you want to do it that way??
i never did something like this , but i guess if you want to execute something on the server other than web page you could put the php code in a file and execute it without browser
Hi w_3rabi,
Thankyou for your reply. Actually I made an application that users(doesnt have sessionid or registration in site) just come to site and add the products to their list. I maintained a temporary table which contains the products lists. If the users selected the products and send mail to me then the temporary table will clear. otherwise the user just selected the products and didnt send the mail to me and close the window then the table doesnt clear....... so only i want to erase contents in table once the user closed the window.
i think the detail may be just brief.... sorry.
thanks...
mahe4us
Junior Poster in Training
55 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
Ok having read what you are trying to do, I would just place the information in a cookie and then if they visit my site again the first thing that I do is delete the cookie to clear the information.
Hi UrbanSky,
Thankyou for your nice reply. I also have the idea to run a cron job to delete the contents in temporary table. But I would like to delete the contents without using cron job in the table. How could you call a function call for this purpose? may i know that...
thanku
mahe4us
Junior Poster in Training
55 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
Hi w_3rabi,
Thankyou for your reply. Actually I made an application that users(doesnt have sessionid or registration in site) just come to site and add the products to their list. I maintained a temporary table which contains the products lists. If the users selected the products and send mail to me then the temporary table will clear. otherwise the user just selected the products and didnt send the mail to me and close the window then the table doesnt clear....... so only i want to erase contents in table once the user closed the window.
i think the detail may be just brief.... sorry.
thanks...
Why not just delete the temporary table when the users session is deleted?
(or when session expires).
Or you could just have a query that would check for temporary tables that have expired, and run the query every time a new table is created.
eg:
[PHP]$sql = "SELECT id from temp_table_name where update < 'NOW() - 3600'";[/PHP]
or if you have a db based session:
[PHP]
$sql = "SELECT t.id as tableid from temp_table_name as t LEFT JOIN session_table_name AS s ON (s.id = t.sessionid) where s.expires < NOW()";
[/PHP]
You don't really need to use cron. If you're worried about server/db load, just cache the last "cleanup" time to a file. Then If the last cleanup time was over 1 hour or so, runt he "cleanup" query again.
One problem I can see with using the window onunload event is that it doesn't tie into a users session. A user may have multiple windows open. Then may have not meant to close the window, and they still want their list of products there.
An expiration time linked to the session is better.
The window.onunload event with a new XMLHTTPRequest call does work though if you're sure the user only has one window open, and the one window represents a session. (common with Web2.0 designs).
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
Hi digital-ether,
Thank you for your great reply. I really get some ideas from your reply. I have store the each sessionid obtained from session_id() in the temporary table. As you told I just check the sessionid available in the table and check whether it is active or not. once it not active then i delete it using your query.
Thank
mahe4us
Junior Poster in Training
55 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
I simply would use an expiring session cookie.
That way, you wouldn't have to worry about the database usage and cron jobs.
Usually, it isn't a good idea to store temporary information on the server.
Ditto.
BTW: why did this thread get resurrected after two and a half years?
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080