Page 1 of 1

deleting a record if browser close

Posted: Wed Jun 11, 2003 12:08 pm
by krummels
Hi All

I designed a shopping cart using sessions but there is a little problem or my sessions is not working properly.

If a user add items to his/her cart the session_id is being entered in a mysql db with the list of items,now the problem.....

If the user decide to close the window without submitting/cancelling the order the session is destroyed of course but the record of the items still exits in the db

can someone help me with a script that deletes the record where the session_id = $session_id as soon as the user closes the window or when the session is destroyed ??????

the interface is working perfectly but I need the script to get rid of ....well "false" orders existing in the database

Thanks a lot!!

check out this thread

Posted: Wed Jun 11, 2003 12:18 pm
by phpScott
Check out this thread as they are talknig about some thing very similar.

http://www.devnetwork.net/forums/viewto ... highlight=

phpScott

Posted: Wed Jun 11, 2003 12:25 pm
by cactus
I'm a little confused by your statement:
.......script that deletes the record where the session_id = $session_id as soon as the user closes the window or when the session is destroyed ??????
Do you already destroy the sessions ? If so what's the issue ?

If your sessions still exist after your/the expiry time you need to create a garbage colelction method that simply runs a query on your db to delete those expired sessions:

DELETE FROM blah WHERE expiry < NOW();

How you run this is upto you, but a cron (if you have access) would be my choice.

Remember if you have a lot of sessions and don't run InnoDB tables then your query WILL lock the db/table, may not be a problem but at high load you may experince and issue or two.

Regards,

Posted: Wed Jun 11, 2003 1:19 pm
by JPlush76
if you're using a database I would set a cookie with a session id then keep that session id linked to the database

that way if they accept cookies when they come back all their stuff is still in their cart. I use that for my site because schools like to build orders slowly.

Posted: Wed Jun 11, 2003 1:25 pm
by cactus
In your situation sessions arn't really applicable, as you are using your sessions as an "account" for an anonymous user.

Regards,

Posted: Wed Jun 11, 2003 1:29 pm
by krummels
Thanks for the replies.

I'm not that good with php yet, so maybe its just stupid me :-)

This is the code deleting the session and removing the items from the cart on the send order page...

if(isset($sessionId)) {
$message = "<p>End of session ($sessionId).";
session_start( );
session_destroy( );
} else {
$message = "<p>There was no session to destroy!";
}

//remove items from cart
$rm_item_sql = "DELETE FROM session_track WHERE USER_ID = \"$sessionId\"";
mysql_query($rm_item_sql) or die ("Couldn't delete item");


but on the checkout page
you'll fill in you name ,address credit card details for example

What if the user closes the window of the checkout page...The ordered items will still be sitting there in the db to keep track:-)

How do I get rid of those if the window is closed?? or is the session suppose to do it?

Posted: Wed Jun 11, 2003 1:40 pm
by cactus
If you can clean out sessions via cron, do it when the next session is created (i.e. when a new user enters your site).

Before creating a session run the grabage collection, the only real worry with this is that you may have a lot of sessions to delete or your server is slow therefore your "new" user has to wait for the garbage collection to end before he/she gets a session.

It all depends on the amount of traffic (generally greater that 150 pages/sec), speed of your server and the number of expired sessions.

Regards,

Posted: Wed Jun 11, 2003 1:42 pm
by cactus
Appologies, the first line should be "If you CAN'T use cron".

I'm hungry, need to get some food ;)

Regards,

delete as soon as session destroy

Posted: Thu Jun 12, 2003 6:00 am
by krummels
Can I use code looking like this more or less

if(session_destroy()) {
//Create connection
$connection = mysql_connect("database", "user", "pass")
or die ("Couldn't connect to database.". mysql_error());
//Select database
$db = mysql_select_db("database", $connection) or die ("Couldn't select database");

$rm_item_sql = "DELETE FROM session_track WHERE USER_ID = \"$sessionId\"";
mysql_query($rm_item_sql) or die ("Couldn't delete item");
}

this code destroys the session and deletes the record from the db but doesnt seem like it reading the if statement (it should Delete only IF the session is destroyed) I'm sure theres just a little error in the script ??

Posted: Thu Jun 12, 2003 6:19 am
by cactus
I think I'm getting a little confused, if your sessions system is based on a database rather than cookies/files then you must be using the session_set_save_handler() to allow you to use a dB.

If so then you just need to overwrite the default session_destroy() method with your own, which contains your SQL to delete the expired sessions.

To confuse things more, are you using file based sessions and then writing the session ID to a database?

Or have I totally lost the plot/thread! ;)

deleting from the db

Posted: Thu Jun 12, 2003 7:09 am
by krummels
Hi,

This just confuses me aswell :)

I'll go and look for more documentation on sessions to explain it

Thanks for your help

Regards,

Posted: Thu Jun 12, 2003 7:44 am
by Judas
Try ading a few Js script lines wich executes the function(s) ya wana.
(you know onload onerror onunload etc.)

or

also an extra field like 'status' for the confirmed orders comes out very handy as well, when you delete the fake orders

Removing Session from Shooping Cart

Posted: Thu Jun 12, 2003 6:34 pm
by skehoe
You could try something like this:

<BODY onUnload="window.open('clear_session_data.php?sessionId=xxxxxxxxxxx')">

And then put the your query in the clear_session_data.php:

$rm_item_sql = "DELETE FROM session_track WHERE USER_ID = \"$sessionId\"";

Maybe echo out something like: Shopping Cart Cleared...

Just a thought.

~Scott

deleting on window close

Posted: Fri Jun 13, 2003 5:33 am
by krummels
Hi, I tried to use <body onUnload....

If I can remember correctly..

when the user refresh the checkout page to confirm the amount of items ,the onUnload will delete the data

I'll get it right sooner or later :)

Thanks again

Posted: Fri Jun 13, 2003 5:48 am
by cactus
As a tip, try drawing out what your system should do as a flow diagram, that way you will better understand the flow of data and user intraction and choose the best place for your clean up process.

Regards,