Reputation: 1399
I have created a shopping cart on a project for college using PHP, and I can add to cart with a link from the items, this updates my cart table in my database which i then print out.
I want to be able to delete eveything in the table once the user has exited the page or closed their browser (i presume this is when the session id changes).but i cant think of the logic to doing this?
cheers
Upvotes: 0
Views: 842
Reputation: 1059
Usually this is done keeping track of the user's last activity.
Every time any user opens a page a last_log
entry for the all the logged users are checked on the database. If the last time one user did something is older than N minutes, then the session is made to expire. You can then log the user out and remove its items.
You might rise the question: "What happens if there's no user currently opening pages? There will be no one to trigger the execution of the code". In that case there's also no one interested to know the content of any shopping cart and the table can remain filled. To solve this problem, with this solution, you might need a scheduled task running on the server.
Here's a summary of the steps:
...
Retrieve the list of expired sessions since the last check
For every expired session
Remove the items from the shopping cart
Update the last_log entry for the current user
...
Checking all the users sounds like an expensive operation, but you have to consider few things:
SELECT
to find who to log out (if you need it) and one UDPATE
to log them outHere is a sample SQL (in no particular language):
-- Used to check which user session have expired since the last check
SELECT username FROM session_table WHERE last_log < SUBTRACT(DATE(), 30 MINTUES) AND logged = 'true';
-- Used to log out the users after the timeout
UPDATE session_table SET logged = 'false' WHERE last_log < SUBTRACT(DATE(), 30 MINTUES) AND logged = 'true';
-- Used to update the last_log entry for the current user
UPDATE session_table SET last_log = DATE() WHERE user_id = "...";
You can do this using cookies or sessions, but I prefer the DB as it avoids to deal with PHP session tracking and allows you to do more complex things if, for example, you want to keep a "saved for later" or "draft" item list.
UPDATE: I made some clarifications about the logid and the DB/SQL part.
Upvotes: 1
Reputation: 714
use JavaScript to send an AJAX request to your server when user close browser you can check with Javascript event "onBeforeUnload". This way the script will ensure the session and DB record are only deleted when the user is leaving the website.
<script type="text/javascript">
$(window).on('beforeunload', function() {
$.ajax({
url: /yourfileDeleteSession.php
});
});
</script>
Upvotes: 0
Reputation: 143
You can "clear" your table using some time-logic.
For instance, when you user come back to your page (you can verify it using $_SESSION, $_COOKIE, $REMOTE_ADDRESS and so on), if that 'second' view is after, maybe 2 days, you can 'clear' those datas. Or, you can create a 'job' inside your database to check when was the last time the user access your webpage (column 'last_login') and again, delete any product inside the card, which that user added.
Upvotes: 0