Timer?
Moderator: General Moderators
Timer?
Hi,
I need bit of help. I have a shopping cart where user can do online shopping. Whenever user select any item I put it in user session. I only want to keep any itme for 20 minutes. So if the user did not check out with in next 20 minutes of selecting item the item should be removed from user session. Any help
Regards
I need bit of help. I have a shopping cart where user can do online shopping. Whenever user select any item I put it in user session. I only want to keep any itme for 20 minutes. So if the user did not check out with in next 20 minutes of selecting item the item should be removed from user session. Any help
Regards
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
Couple of options...
If using sessions expire the session after 20 minutes of inactivity - Not necessarily the best idea since if user has logged in may not want to be logged out.
Store the cart with a timestamp. Every change to the cart resets the timestamp. If timestamp + 20 mins > current timestamp reset. If stored in a session the cart is only held in memory so no problem with redundant carts. You simply need to check whenever you access the cart if it has expired. If using a database for storage you need to clean up any redundant carts which have not been completed.
If using sessions expire the session after 20 minutes of inactivity - Not necessarily the best idea since if user has logged in may not want to be logged out.
Store the cart with a timestamp. Every change to the cart resets the timestamp. If timestamp + 20 mins > current timestamp reset. If stored in a session the cart is only held in memory so no problem with redundant carts. You simply need to check whenever you access the cart if it has expired. If using a database for storage you need to clean up any redundant carts which have not been completed.
Thanks could you please eloborate bit more. As I am only using session and storing cart in the session. i want item to be removed automaticaaly after 20 mins if the user has not checked out.CoderGoblin wrote:Couple of options...
If using sessions expire the session after 20 minutes of inactivity - Not necessarily the best idea since if user has logged in may not want to be logged out.
Store the cart with a timestamp. Every change to the cart resets the timestamp. If timestamp + 20 mins > current timestamp reset. If stored in a session the cart is only held in memory so no problem with redundant carts. You simply need to check whenever you access the cart if it has expired. If using a database for storage you need to clean up any redundant carts which have not been completed.
Thanks
- Gente
- Forum Contributor
- Posts: 252
- Joined: Wed Jun 13, 2007 9:43 am
- Location: Ukraine, Kharkov
- Contact:
When you update your cart set the session var:
And when you want to check use following
Code: Select all
$_SESSION['last_cart_updated'] = time();Code: Select all
if (isset($_SESSION['last_cart_updated']) && (time() - $_SESSION['last_cart_updated'] < 20 * 60))
{
// Process the order
}
else
{
// Remove your session vars
}Gente wrote:When you update your cart set the session var:And when you want to check use followingCode: Select all
$_SESSION['last_cart_updated'] = time();Code: Select all
if (isset($_SESSION['last_cart_updated']) && (time() - $_SESSION['last_cart_updated'] < 20 * 60)) { // Process the order } else { // Remove your session vars }
I want to remove the itme automaticaaly after 20 mins not when the user press check out.
Aint any problem. Thing is what I want to do is :miro_igov wrote:And what is the problem with Gente's code to do it?
When user select any item I will put that itme in user session. Now that item should be removed automatically after 20 mins. User might be staying on same page for 20 mins after selecting the item . Then I cant remove the item actually I dont wanna waite for user to refresh the page . If the user move to other page then I can check the item validity but still if the item is not 20 min old then how can I checked it again.
How I can access session variables from javascript.miro_igov wrote:You can put the session check in top of every page.phpcoder wrote:... but still if the item is not 20 min old then how can I checked it again.
But if you need items disappear without page load then use javascript to hide the container of the item after specific timeout.
Thanks
strange, you tell onion2k that sending a request to the server is not an option and then you advice phpcoder to send a request to the servermiro_igov wrote:@ onion2k: he wants the items disappear without to interact with the server (page reload).
@phpcoder: with AJAX, send request to a script which does session dump or outputs the required session variables.
Yes, I understand that. He's being a bit silly, and is about to start building a really inaccessible shopping application. Far be it from me to dissuade him though. The worst part of it is that he's in the UK, so he'll potentially be breaking the law (depending on what he's selling and whether there's any other ways to buy it).miro_igov wrote:@ onion2k: he wants the items disappear without to interact with the server (page reload).