tags:

views:

91

answers:

5

i m creating two table(in mysql) named

  1. table_temp_guest
  2. table_temp_order

now if a guest enters then we save his personal information in this first table and if he purchase something from any stall ,it saved as a temporary order in table_temp_order.

now my question is :

i m using session id, so when user goes to logout( without checkout) then i delete his information(personal and order) from both table )using session id, BUT if he close the browser, or does not go to checkout(any reson) then how to delete his information from both tables please suggest me how to do this?

additional question: is there any other way to do this whole process by some other manner.

+4  A: 

You can't detect when a user closes the browser or types in a new address. You basically need to have a "timeout" facility like the rest of the websites have.

Philluminati
and ho do we manage timeout facility? will u please tell me
diEcho
Use a "last_updated_at" field on either table, and periodically (say, once every day) delete all entries with last_updated_at entries far in the past.
Joel L
+2  A: 

There is a window.onunload event that you can detect with javascript, but it's not universally supported, and it detects window closes, not browser closes.

Your best resolution is probably going to be tracking the session_id and last accessed date. Re-update the table's last_accessed_date on every page load, and delete everything that's older than a few hours.

davethegr8
but at the same time many guests are on the site from diffrent PC then how can i remove that specific user's information by last accessed date??
diEcho
If there is more than one guests using the computer at the same time without closing the browser window (or one after the other)? There's not really any way. A php session is unique per visit and browser. If you visit amazon, add a bunch of stuff to your cart, and then walk away from the computer, the next person would still be able to see your (unfilled) order. This is why everyone says "Remember to close your browser when you're done" on public computers.You could have a logout link for guests that destroys the session data.
davethegr8
user is closing browser, but now logging out dear
diEcho
If you're on windows, closing the browser is equivalient to quit, and the next time someone visits the site they will get a new unique session id.
davethegr8
+1  A: 

A timeout would be the best method.

Record the last active time in the guest table. Have a cron job running periodically on the web server cleaning up sessions that exceed the maximum time that you wish to allow.

Be careful about the amount of time that you allow. You have to allow for slow users and dropped connections.

Buggabill
i don't know how to set cronjob in php, will u plese tell me the process or simple tutorial site? thank you
diEcho
You can't set them in php; you would have to write a shell-script (for whatever shell your specific server is using), and insert into the cron-list for that server. If you need help with that, I'd recommend posting another question about cron-jobs and how to write their entries.
macabail
Some hosts have a pretty easy interface for entering jobs. It all depends on your host. You can write the script in php and call it with /path/to/php yourscript.php in the job. Most of the time the paths to certain executables are published by the web host. If you have shell access, it is easier, because you figure out the path yourself. I think that phpinfo() even shows the executable path.
Buggabill
A: 

Also you can try to have a script that would run on the client side and ping the server so that you know if the script has not pinged for a while, the user closed the browser. That being said, I would agree with the previous posters, a timeout/ cleanup procedure would be best.

For that you would add a ModifiedDate field to your tables, you can set it as an "ON UPDATE" field for ease of use, then just delete all records that have an ModifiedDate field of older then several hours.

Dmitry Grekov
+1  A: 

If you're using session_id() anyway (I guess this is what you mean by session id), just use php sessions. PHP automatically invalidates them for you and you don't need those two tables (you can store everything you need in $_SESSION).

There is no way to check if the broswer wasn't closed you could rely on.

If you don't want to change the way your project works now, just add a created field to the tables and set it to the current time() whenever you're "seeing" the specific user. Then set up a cronjob which deletes all records from this table which are older than a specific timeout.

svens
by cron job can we delete that specific user's detail( by using session id) or that cron job empty the table?? please suggest me sgood site for cron job in windows(Xampp)
diEcho