Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-03-2012, 08:42 PM   PM User | #1
Thyrosis
New Coder

 
Join Date: Nov 2012
Posts: 72
Thanks: 4
Thanked 11 Times in 11 Posts
Thyrosis is on a distinguished road
Question (probably) another session question

Hello all,

Currently I'm working on a project which includes the possibility to buy things on the website. Once you click on something like 'buy it now', the item gets assigned to you (the user), so that other users cannot buy it.

Now, I've got that all worked out. The item-object has a field status in the database which is 0 for open, 1 for reserved or 2 for bought. Once I buy the item, no one else can click on 'buy it now', and once I have paid, the status changes to 2 and everybody is happy.

But, where I am stuck at is the bit in the middle. How do I go about setting a time-limit to the reserved status? That once I have clicked the button, I need to complete the payment in say 20 minutes? And if I don't complete it within those 20 minutes, the item gets put back on the market with status=0? Or, if I don't fancy buying it anyway and just press the browsers 'back' button to return to the items overview, how do I intercept that and change the status to 0 immediately?

One option is to have a separate table to contain 'held' items, which is being checked every time someone visits a page. If the query finds an item which has been put on hold for longer than 20 minutes it will change the status back to 0. That's all good fun for the 'hmm I'm taking too long to pay' thing, but it won't put an item back on the market as soon as someone decides to skip the sale.

I have been searching for it the whole evening, but didn't get very far. I looked at Claweds' thread too, which pretty much handles the first 20 minutes check, but doesn't involve the second bit.

If you do have a lead I can follow, please let me know.

Many thanks in advance,
Martin
Thyrosis is offline   Reply With Quote
Old 12-03-2012, 09:58 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
If it were me, I'd add a column to your table alongside your status called status_changed. This would store a timestamp that gets updated to current datetime any time the status changes.

Then the tricky part comes in what mechanism do you use to check that timestamp and take action when 20 minutes is up? Well you can't count on the client at all. He could simply turn off his PC, you'd never know (unless you had Ajax pinging you back, which is usually not a good solution).

So, how about a CRON job that runs every minute? It would do just the one task: query the entire database table looking for rows with a "reserved" status and a status_changed timestamp older than 20 minutes. If it finds anything, update the status back to available and maybe send an email to the buyer that he missed his chance at buying the item.
__________________
Fumigator is offline   Reply With Quote
Old 12-04-2012, 11:03 AM   PM User | #3
Thyrosis
New Coder

 
Join Date: Nov 2012
Posts: 72
Thanks: 4
Thanked 11 Times in 11 Posts
Thyrosis is on a distinguished road
Hi Fumigator,

Thanks for the reply! I hadn't thought of that solution, it also adds the possibility to track when the item was bought, double bonus!

The cron job would be possible, the process will never take longer than a minute so it won't get into trouble with itself.

Remains the issue of the user hitting the browsers back button. Any ideas?
Thyrosis is offline   Reply With Quote
Old 12-04-2012, 02:39 PM   PM User | #4
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
The page the user lands on when they hit the back button, "product overview", you called it, is where you'll need to evaluate reserved items. Make a call on that page to a function that checks to see if the user has any items reserved, and take action if you find something-- either dropping the reservation, or maybe even alert the user they have an item reserved, do they want to cancel it? Something along those lines.
__________________
Fumigator is offline   Reply With Quote
Old 12-04-2012, 03:24 PM   PM User | #5
Thyrosis
New Coder

 
Join Date: Nov 2012
Posts: 72
Thanks: 4
Thanked 11 Times in 11 Posts
Thyrosis is on a distinguished road
Brilliant! Now why didn't I think of that myself... Thanks!
Thyrosis is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:44 PM.


Advertisement
Log in to turn off these ads.