How to prevent adding twice on booking system - php

I have an booking system time, where the user can book a time let say an hour within a specifik date. My question is i cant handle the case when for ex i open in two tabs the site and i try to book a time from one tab and then it displays on the calendar that the booking is done, but since in the other tab the page is not being refreshed and it shows still avaibale the time to be booked and i can book it, so then it shows me two, i mean i can open as much tabs as i can and do the same, how could i prevent this to be able only one time in a specific date to be booked???

You can write some script which will returns aviability of any period by request. Than, in your booking page make a simple Ajax query to check if selected period is still available and run it by setTimeout(). If request will shows you that period is already booked you can notify user by showing him relevant message and disable form submition until valid period will be chosen.
this will also helps you in case when someone will book selected date while other user is thinking of it :)

Related

How to handle same time request with Apache and MySQL [duplicate]

I am writing a hotel booking software using PHP and MySQL. I am pretty much done with it but I ran into a race condition problem. For example there is only one standard room left and when 2 guests both select it it shows available to both of them. I tried fixing it by checking the room again when the guest clicks confirm before payment but that still has issues. I also tried making the room status to pending when whoever clicks the confirm first but I can't figure out how to change it back to available if the guest decides not to pay or just closes the browser. I searched SO for answers but I didn't really find a definitive answer. Thanks in advance
One solution is to add two columns to a table in the database. One column is the session ID or user ID or whatever of the user that is being offered the room. The second column is a timestamp indicating when that offer will expire.
Then, in your app, only show rooms that have an expired timestamp in the hold column. (Set the initial timestamp to 0 so that it starts out expired.) When a room is selected, check the column again. If there's an unexpired timestamp there, the user gets a "sorry, you were too slow" message. Otherwise, put a timestamp there for 15 minutes into the future or whatever, and proceed.
You see this on travel sites and ticket-purchasing sites a lot where it says something like "We're holding these seats for you for another 14 minutes. Please complete the transaction by then or it will be released blah blah blah."
I would also go with the pending state. You could save the state together with the session id of this user and have a cronjob that deletes all pending states that have expired session ids associated to them.

How can my session update new changes every time my website is open?

Description:
In my website, there are cash amounts for every person with an account.
By the press of a button, a certain amount of money subtracts or adds from the cash amount.
Problem:
This change only shows in the database (phpmyadmin), but when I write the cash amount on my site, it still shows the old amount. The amount only updates when the user logs out and then back in. I want the amount to automatically update when the page is open.
Question:
Is there a line of code that i can add to my php at the top of the page after session_start(); , such as session_update(); (I know this doesn't exist, it's just an example) or something like that to check for the changes? Or will it have to be a complicated several lines of code? I want to do this without making the person log out.
Thanks for helping! (i'm kind of a noob at this :) )
Let's say you have a session variable of $_SESSION['cash_amount'] and it currently equal $100. If $10 gets subtracted, then in your code, you just say $_SESSION['cash_amount'] -= 10; and it will subtract that from the session variable so next time the user sees the page, it will have the correct amount. Of course you're probably pulling this from the database and if that's the case, then you want that session variable to be equal to the result of the query you're pulling from.
And if you want to do this without refreshing the page, then you'll use AJAX and pull the amount in that way but that's a whole different ball of wax that you might not be prepared for yet.

PHP deadline notification

I need to create a system that can store documents, those documents are jobs due to specific dates so this system should send an email notification to the admin to notify him/her when a due date for a specific document is near.
Basically i have no problem at all, apart from one single very important point: the notification system should work without user intervention. I can easily trigger php scripts on each page visit but i do not want that. This case would be quite "easy", i would just set a date for each document in the sql row and each time the page is visited a php script would check if the current time is near the time set in the sql row for the document. But this needs the page to be visited.
Suppose my client sets a due date for a document and then never visits the site again, how can it happen that a php scripts automatically fires itself to perform the necessary checks to see if there are due dates in the upcoming days?
Thanks in advance
Why not use MySQL Event schedular?

Update information at specific timestamp

I'll try to explain my question the best way I can.
I'm not asking for codes, only for the best method of doing it.
I want to create a browser game and use time for upgrading stuff, building etc.
For example, to build 1 house will take 1 hour.
So I will began with saving the timestamp+(60*60) at the moment the user did his action.
My question is, how to update it the best way?
One way I thought of was to add function that check every page view of the user if it's done.
But then if he's not logged in the update wont happen.
Second way i thought about is for every page view of any user to check for every user registered. But it's not effective and there is a problem if no user is logged in.
Any suggestions?
I had my game doing it simply, without crons.
When a player wanted something that takes time, i just updated his database information with the appropriate time of ending that job (columns are just examples)
UPDATE player SET jobend = UNIX_TIMESTAMP() + (60*60*4) # ending in 4 hours
Then, every page that had an information about the remaining time, i just used something like this:
SELECT (jobend - UNIX_TIMESTAMP()) AS jobremaining FROM player
I formatted correctly the time using strftime and i displayed that to the user.
In the case the remaining time was negative, the job was done.
There was no-need for absolute counting since user was able to do something with the job when he was connected.
When the player just changed pages or doing something else i had a function where i just checked all timely events while the user was online (so to catch any negative timer), then presented with javascript any change (i posted javascript counters for every page)
Now, if you talk about updating in real-time, cron is the way but are you sure you're going to need it for a game? I asked that question myself too and the answer was not.
EDIT
If another player sees the buildings on schedule page (an hypothetical page) i am doing the same calculations; if a time just got negative for a specific player (regardless if another player see the page), i just reward him with the building (in database i make all the changes), even if he's offline. There's no harm in this, since he can't do anything anyway. The other players will just see he has a building. The key here is that i execute the required updating PHP code regardless of player's connection to the game; as long at least ONE player is logged-in i'm executing the progress function for everything.
This isn't so slow as it sounds (updating all players by using just a connected player that visits a specific page). You just have a table of 'jobs' and check timers against the current time. More like a single query of getting the negative ones.

slot booking problem

I am making a doctor appointment slot booking mechanism,where in doctor appointment slots will be divided into 30 mins slot each...i have achieved all the working code.. 1 problem i am facing is that..this booking is made at 2 places i.e 2 receptions..so when 1 selects a slot(radio button) not yet confirmed and saved in DB.other reception must not be able to select .how do i do it.any help on this...how do i go abt it.
This is a case of accessing "shared data". You'll need thread to make sure that only one thread has access to the data at a time to ensure it's integrity. The following might provide some ideas
http://www.alternateinterior.com/2007/05/communicating-with-threads-in-php.html
I would use some AJAX/AJAJ functions to periodically refresh data about free slots, or I would do that much more simple - when saving the appointment, just check it, if the slot will be taken, your app redirects user back to form to choose another slot.
In essence,
I will just ask for the slot timings upfront and the remaining details later.
In case the slot is available, it sends a request to the server to lock it, so that the other client cannot use it.
In case, it is not available, it will receive a small notification that this slot is unavailable, click to see available slots.
I would go with AJAX (if this is a web app). This is a distributed systems problem which resembles Blue Army - White Army problem.
Add a "Locked By" field to the table.
When booking a slot, do something like:
UPDATE tablename
SET LockedBy = userid, ...
WHERE LockedBy IS NULL
After the update, you can select to see if LockedBy is set to your userid.
If not, then someone else must have beaten you to the punch, and you need to tell the user to pick a different slot.

Categories