简体   繁体   English

PHP-标头重定向后使用sleep()

[英]PHP - use sleep() after a header redirect

I have a complicated setup for a shop situation. 我有一个针对商店情况的复杂设置。

People can buy unique products in a short time frame and pay via PayPal. 人们可以在短时间内购买独特的产品并通过PayPal付款。 So I need to be sure the products are not bought multiple times. 因此,我需要确保不会多次购买产品。 For that purpose on checkout I am using a custom PHP file that updates the items in the database to a "reserved" status and then sends the form to PayPal via header() . 为此,我使用了一个自定义PHP文件,该文件将数据库中的项目更新为“保留”状态,然后通过header()将表单发送给PayPal。 This is all working very nicely. 这一切都很好。

Because we have limited items to sell and such a limited time frame, I want to make items available again if the buyer aborts the payment process. 由于我们要销售的商品数量有限,并且时间限制也很有限,如果买家中止付款流程,我想再次提供商品。 I do have PDT set up and running but the transaction protocol sometimes comes with extended delay. 我确实已经设置并运行了PDT,但是事务协议有时会延迟。 So ten minutes after the checkout I want to check the database to see if the transaction came through. 因此,在结帐后十分钟,我想检查数据库以查看事务是否通过。 If the item is really sold (status is changed by the PDT script) all is fine, if it is not sold, I want to reset the reservation status and make the item available for other buyers again. 如果该商品确实已售出(状态已通过PDT脚本更改),则一切都很好,如果未售出,我想重置预订状态并使该商品再次可供其他买家使用。

Here is the setup of my code: 这是我的代码的设置:

if ($resstatus == "0"){
    $result = mysqli_query($mysqli, "UPDATE `table` SET `Reserved`='1' WHERE `AID`='$AID' AND `itemID`='$itemD1'");
    header('Location: https://www.paypal.com/cgi-bin/webscr?'.$post_string);
    sleep(600); //wait ten minutes before checking DB
    if ($sold == "0"){
        $reset = mysqli_query($mysqli, "UPDATE `table` SET `Reserved`='0' WHERE `AID`='$AID' AND `itemID`='$itemD1'");
    }
}

Everything is executed, but the header redirect is also delayed the time span defined in sleep() . 一切都已执行,但标头重定向也延迟了sleep()定义的时间跨度。 This doesn't make sense to me at all. 这对我完全没有意义。 If it would redirect and NOT execute anything afterwards - that's one thing. 如果它将重定向并且之后不执行任何操作,那是一回事。 But it executes everything but behaves as if the order was first sleep() and then header() . 但是它执行所有操作,但表现得好像顺序是先sleep()然后是header()

I am going for a different approach now by saving a timestamp on checkout and checking for units with reservation-timestamps older than ten minutes when the shop is loaded. 我现在打算采用另一种方法,即在结帐时保存时间戳,并在加载商店时检查保留时间戳早于十分钟的单元。 That should work and is probably more elegant. 那应该可行,并且可能更优雅。

But I'm still puzzled by the above behavior and would like to know where my error in reasoning is buried. 但是我仍然对上述行为感到困惑,想知道我的推理错误被埋在哪里。

It depends on the SAPI you are using. 这取决于您使用的SAPI。 Nevertheless you can not count on the code after sleep to be executed and should really be using a cron job that checks the timestamps and removes the reservations. 但是,您不能指望睡眠后要执行的代码,实际上应该使用cron作业来检查时间戳记并删除保留。

Especially since you have such a big timeout the server would probably terminate the long running script. 尤其是因为您有这么大的超时时间,服务器可能会终止长时间运行的脚本。 Also, your script while sleeping is also tying one of the processes that could be used for serving other content. 同样,您的脚本在睡眠时也会占用可用于提供其他内容的进程之一。

BTW, this would probably work: 顺便说一句,这可能会工作:

ob_start();
header('Location: https://www.paypal.com/cgi-bin/webscr?'.$post_string);
ob_end_flush();
flush();
ob_end_clean();
sleep(600);

See also here 请看这里

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

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