djlfreak Posted June 4, 2010 Share Posted June 4, 2010 How do I word this function so the cart contents are emptied when user clicks log out button? I'm thinking it would be something like WHERE user_id= $_SESSION['']; function deleteAbandonedCart() { $sql = "DELETE FROM tbl_cart WHERE ?????? '"; dbQuery($sql); } ?> //LOGOUT FUNCTION case 'Logout': session_start(); session_unset(); session_destroy(); redirect('index.php'); break; Thanks in advance for any help Last post I promise, just trying to finish off website and had a few issues can't fix. Quote Link to comment Share on other sites More sharing options...
ignace Posted June 4, 2010 Share Posted June 4, 2010 Where do you store your cart contents in session? db? or both? You must realize that very little people actually press the logout button. So if you base your logic on this event then in most cases you won't be clearing at all. Quote Link to comment Share on other sites More sharing options...
djlfreak Posted June 4, 2010 Author Share Posted June 4, 2010 I store cart contents in session id like this // current session id $sid = session_id(); // check if the product is already // in cart table for this session $sql = "SELECT pd_id FROM tbl_cart WHERE pd_id = $productId AND ct_session_id = '$sid'"; $result = dbQuery($sql); At the moment it deletes cart entries older than one day but I want them to delete immediately once user leaves site, not logs out as you can shop without logging in. How would I achieve this? /* Delete all cart entries older than one day */ function deleteAbandonedCart() { $yesterday = date('Y-m-d H:i:s', mktime(0,0,0, date('m'), date('d') - 1, date('Y'))); $sql = "DELETE FROM tbl_cart WHERE ct_date < '$yesterday'"; dbQuery($sql); } The reason I'm asking is when I was testing it logging in as different users the cart from old user was still there. Quote Link to comment Share on other sites More sharing options...
ignace Posted June 4, 2010 Share Posted June 4, 2010 Don't store products in the database but keep them in the session. The cookie is by default set to expire as soon as they close the browser. Which means the next time they visit your website their cart will be empty. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.