Timer?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
phpcoder
Forum Contributor
Posts: 158
Joined: Sat Nov 02, 2002 1:18 pm
Location: Manchester, UK

Timer?

Post 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
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post 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.
User avatar
phpcoder
Forum Contributor
Posts: 158
Joined: Sat Nov 02, 2002 1:18 pm
Location: Manchester, UK

Post 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
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post 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
}
User avatar
phpcoder
Forum Contributor
Posts: 158
Joined: Sat Nov 02, 2002 1:18 pm
Location: Manchester, UK

Post 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.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

And what is the problem with Gente's code to do it?
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post 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...
User avatar
phpcoder
Forum Contributor
Posts: 158
Joined: Sat Nov 02, 2002 1:18 pm
Location: Manchester, UK

Post 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.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post 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.
User avatar
phpcoder
Forum Contributor
Posts: 158
Joined: Sat Nov 02, 2002 1:18 pm
Location: Manchester, UK

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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).
Post Reply