deleting a record if browser close
Moderator: General Moderators
deleting a record if browser close
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!!
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
Check out this thread as they are talknig about some thing very similar.
http://www.devnetwork.net/forums/viewto ... highlight=
phpScott
http://www.devnetwork.net/forums/viewto ... highlight=
phpScott
I'm a little confused by your statement:
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,
Do you already destroy the sessions ? If so what's the issue ?.......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 ??????
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,
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?
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?
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,
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,
delete as soon as session destroy
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 ??
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 ??
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!
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
Hi,
This just confuses me aswell
I'll go and look for more documentation on sessions to explain it
Thanks for your help
Regards,
This just confuses me aswell
I'll go and look for more documentation on sessions to explain it
Thanks for your help
Regards,
Removing Session from Shooping Cart
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
<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
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
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