简体   繁体   English

在预订应用程序中保存房间

[英]Saving rooms in booking app

I have some website for hotel reservation. 我有一些网站可以预订酒店。 People who are aligible, can choose a hotel, dates and capacity and pay for the order. 有资格的人可以选择酒店,日期和容纳人数并支付订单。

When the client make the decision and clicks the order button a few steps heppens: 当客户做出决定并单击“订购”按钮时,将发生几步变化:

  1. Application check again for price and gathering order information 再次检查价格和收集订单信息的应用程序

  2. Saving rooms for the client to prevent overbooking 为客户节省空间以防止超量预订

  3. A timer set to 5 min for client order window (if time is gone the rooms are released) 客户订单窗口的计时器设置为5分钟(如果时间已过,则释放房间)

If the client goes out the orger page a null_timer is trigered and saved rooms are released 如果客户离开组织者页面,则会触发null_timer并释放已保存的房间

If the client starts order (in order page) he can't procced other orders 如果客户启动订单(在订单页面中),他将无法处理其他订单

  • $_SESSION['order_iddle'] and timer changes triggered from client-side via ajax $_SESSION['order_iddle']计时器更改通过ajax从客户端触发

JS trigger JS触发器

window.onbeforeunload = function(){ 

      timer_null();   
      return ""; 

}

PHP check PHP检查

if ((!isset($_SESSION['order_iddle'])) || (!$_SESSION['order_iddle']) )  {

     header('location: https://home-page/');

}
  • $_SESSION['order_iddle'] updates with timer in php. $_SESSION['order_iddle']用php中的计时器更新。
  • timer_null() sends ajax request to php to update/check timer timer_null()将ajax请求发送到php以更新/检查计时器

Question 1: 问题1:

This logic relies on onbeforeunload() function, but if the function not working in some browser we have trouble. 这种逻辑依赖于onbeforeunload()函数,但是如果该函数在某些浏览器中不起作用,我们将遇到麻烦。 If there a better way to trigger timer_null() when user go away from order page? 当用户离开订单页面时,是否有更好的方法来触发timer_null() Maybe in PHP? 也许在PHP中?

Question 2: 问题2:

When onbeforeunload() triggers and user clicks "Stay on the page", timer_null() triggered anyway. onbeforeunload()触发并且用户单击“停留在页面上”时, timer_null()仍然触发。 Is there something to do with that? 有什么关系吗?

Thanks ) 谢谢

A better option would be to give the order an expiry value in your back end. 更好的选择是在您的后端中给订单一个到期值。 This way you avoid triggering, problems when users don't use JS, problems when they stay on the page etc. 这样可以避免触发,用户不使用JS时出现的问题,留在页面上时出现的问题等。

When they make an order set a value to the current time plus a number of minutes: 当他们下订单时,将值设置为当前时间加上分钟数:

$order['expiry'] = time() + (5 * 60); // sets an expiry time for 5 minutes (5x60 seconds)

Then on a future page which checks whether the order is valid: 然后在以后的页面上检查订单是否有效:

if (time() > $order['expiry']) {
     throw new Exception('This order is not valid');
}

Catch that exception and return it to the user however you'd like. 捕获该异常,然后根据需要将其返回给用户。

Suggestions for wider use 广泛使用的建议

You might have other things in your use case which require, for example, stock to be held for those five minutes. 您的用例中可能还有其他内容,例如,需要保留这五分钟的库存。 However if you incorporate this expiry into your data store as well then it can be used to check expiries on any future orders. 但是,如果您也将此到期日并入数据存储中,则可以将其用于检查任何未来订单的到期日。

If you wanted to not have to be checking thousands of potentially expired records each time a new order is made you could create a sort of "clean-up" script to run automatically. 如果您不想每次下达新订单时都要检查数千条可能过期的记录,则可以创建一种“清除”脚本来自动运行。 If you've not used crontab before I suggest your read up on it, but a script set to run at midnight every day with crontab: 如果您在建议您阅读之前没有使用过crontab ,而是使用crontab将脚本设置为每天午夜运行:

0 0 * * * php /var/www/my-clean-up-script.php

Then in the script itself (pseudo-code): 然后在脚本本身(伪代码)中:

$expired_orders = database_query ( GET orders FROM table WHERE expired_time < current_time)
for each $expired_orders
    database_query ( DELETE order)

Which should work for any queryable data store. 哪个适用于任何可查询的数据存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM