I want to delete a user from database and destroy the session after he has been idle for some time, lets say 5 mins. How can I do that ? For starters, how to check if the user is idle? I need it mainly for when a user closes the browser he would be deleted from database. But it would be a big bonus if I can delete him even when the browser is still open but he has not done anything. I have a little chatroom and it I have a table that holds which user is in which room. When the user closes the browser, I need to delete him from that table. Ask if something is unclear. Thanks.

Recommended Answers

All 10 Replies

If the user closes the browser, then you can trap that with javascript, and use ajax to call a php script.

For idling, you can set aa timer for 5 minutes in ajax, and restart it every time the user does something.

If the user closes the browser, then you can trap that with javascript, and use ajax to call a php script.

I did some searching and found this http://stackoverflow.com/questions/3888902/javascript-detect-browser-close-tab-close-browser . This says that when back button is pressed it thinks user has closed browser. Thats something I dont want. Is there a way to automatically initiate a script only on server side to delete a row from table. Lets say that if the user has not communicated with server for an hour, I want to delete him. I was thinking that mayve I could add a future timestamp column to my users and somehow compare it on server side only. This way I dont need the user to have the browser actaully open. Would that be possible somehow ? Thanks for answers.

Yes. With every action the user does, update a last used timestamp in your database. Then have a cron job running to check for expired timestamps and remove them from your database.

There is a way to do this by overriding the default session handling, but I've never used this before.

How to use that cron job ? I trye to google it but didnt anything I could hold on to. How does it work and what does it work with ? Is it possible to put it in a file and load it in server (If I decide to make an install package for instance)

A cron job is just a scheduled task, that executes a PHP script at regular intervals. You should be able to add cron jobs in the control panel of your web host.

But is there an automated way to do it? For instance if I want my chatroom to be more mobile. Like wordpress installation for example. Can the script be loaded into server with the analog way ? Thanks for answes so far, they have been very useful.

The cron job runs at your server, independent of anything else. You set it up once, then it keeps running until you delete the job. Am not sure if that's what you mean.

I mean that I make an kind of like install for my chatroom. install.php for instance. It sets up the database for my chatroom to use later on. User dont have to do anything, but fill out the inputs. At the final step of the installation everything would be ready to be used. So what I mean is that I want to everyone to be able to set up the chatroom with minimal knowledge of servers or anything else, that it would be set up hidden from the user (the on installing it on their server). I hope I was a little more clear this time, if not well, then lets just leave it at that :)

Yes, it's possible to make such an installer. Am sure there are tools to help you create it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.