Page 1 of 2
Timer?
Posted: Tue Jul 03, 2007 2:10 am
by phpcoder
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
Posted: Tue Jul 03, 2007 3:14 am
by CoderGoblin
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.
Posted: Tue Jul 03, 2007 3:30 am
by Rovas
20 minutes is much too short a minimum of half an hour you should use and I recommend using session expire in the php.ini file.
Posted: Tue Jul 03, 2007 3:31 am
by phpcoder
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 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.
Thanks
Posted: Tue Jul 03, 2007 3:57 am
by Gente
When you update your cart set the session var:
Code: Select all
$_SESSION['last_cart_updated'] = time();
And when you want to check use following
Code: Select all
if (isset($_SESSION['last_cart_updated']) && (time() - $_SESSION['last_cart_updated'] < 20 * 60))
{
// Process the order
}
else
{
// Remove your session vars
}
Posted: Tue Jul 03, 2007 4:09 am
by phpcoder
Gente wrote:When you update your cart set the session var:
Code: Select all
$_SESSION['last_cart_updated'] = time();
And when you want to check use following
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.
Posted: Tue Jul 03, 2007 4:15 am
by miro_igov
And what is the problem with Gente's code to do it?
Posted: Tue Jul 03, 2007 4:30 am
by Gente
phpcoder wrote:I want to remove the itme automaticaaly after 20 mins not when the user press check out.
If you want to do it without page refreshing you can use AJAX to call this script.
But I don't see the sens...
Posted: Tue Jul 03, 2007 4:45 am
by phpcoder
miro_igov wrote:And what is the problem with Gente's code to do it?
Aint any problem. Thing is what I want to do is :
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.
Posted: Tue Jul 03, 2007 5:00 am
by miro_igov
phpcoder wrote:... but still if the item is not 20 min old then how can I checked it again.
You can put the session check in top of every page.
But if you need items disappear without page load then use javascript to hide the container of the item after specific timeout.
Posted: Tue Jul 03, 2007 5:40 am
by phpcoder
miro_igov wrote:phpcoder wrote:... but still if the item is not 20 min old then how can I checked it again.
You can put the session check in top of every page.
But if you need items disappear without page load then use javascript to hide the container of the item after specific timeout.
How I can access session variables from javascript.
Thanks
Posted: Tue Jul 03, 2007 5:46 am
by onion2k
phpcoder wrote:How I can access session variables from javascript.
Either embed them in the Javascript when the page is generated or use AJAX to fetch them from the server.
Posted: Tue Jul 03, 2007 5:46 am
by miro_igov
@ 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.
Posted: Tue Jul 03, 2007 5:49 am
by volka
miro_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.
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 server

Posted: Tue Jul 03, 2007 6:11 am
by onion2k
miro_igov wrote:@ onion2k: he wants the items disappear without to interact with the server (page reload).
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).