如何阻止 PHP sleep() 影响我的整个 PHP 代码?

发布于 2024-08-09 05:38:03 字数 484 浏览 2 评论 0原文

所以,在我的街机上,howlingdoggames.com。我有一个积分系统,每次您访问正在进行游戏的页面时,都会给您一个积分。为了减少滥用,我想进行某种延迟,因此仅在 45 秒后授予。这是我尝试过的方法:

if ($_SESSION['lastgame'] != $gameid) {
    sleep(45);
    $points = $points + $game_points;
    $_SESSION['lastgame'] = $gameid;
}

但这似乎使我的整个网站停止了 45 秒,因为这是在 index.php 中,以及我网站的许多其他代码。

无论如何,我可以隔离那段代码,这样它只会让语句

$points = $points + $game_points;

等待 45 秒吗?

So, on my arcade, howlingdoggames.com. I have a points system that gives you a point every time you visit a page with a game on. To reduce abuse of this, I would like to make some sort of delay, so its only awarded after 45 seconds. Here's what I've tried:

if ($_SESSION['lastgame'] != $gameid) {
    sleep(45);
    $points = $points + $game_points;
    $_SESSION['lastgame'] = $gameid;
}

But this just seems to halt my whole website for 45 seconds, because this is in index.php, along with a lot of other code for my site.

Is there anyway I can isolate that bit of code, so it only makes the statement

$points = $points + $game_points;

wait for 45 seconds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

花伊自在美 2024-08-16 05:38:03

PHP 中(大部分)没有多线程。您可以通过在 Unix 系统上分叉进程来实现此目的,但这无关紧要,因为多线程并不是您真正想要的。您只需要这样的简单逻辑:

$now = time();
session_start();
$last = $_SESSION['lastvisit'];
if (!isset($last) || $now - $last > 45) {
    $points = $_SESSION['points'] ?? 0;
    $_SESSION['points'] = $points + 10;
    $_SESSION['lastvisit'] = $now;
}

基本上仅在上次给分之间的增量大于 45 秒时才给分。

There is (mostly) no multithreading in PHP. You can sort of do this with forking processes on Unix systems but that's irrelevant because multithreading isn't really what you're after. You just want simple logic like this:

$now = time();
session_start();
$last = $_SESSION['lastvisit'];
if (!isset($last) || $now - $last > 45) {
    $points = $_SESSION['points'] ?? 0;
    $_SESSION['points'] = $points + 10;
    $_SESSION['lastvisit'] = $now;
}

Basically only give the points if the increment between the last time you gave points is greater than 45 seconds.

痴者 2024-08-16 05:38:03

它是您的脚本的会话块。而不是“PHP 中没有多线程”。
sleep() 之前的 session_write_close() 将解决整个脚本的阻塞问题。但可能不适合你的问题。

所以你必须使用js和AJAX的settimeout来保存奖金。

来自php.net中sleep()的注释:
http://www.php.net/manual/en/function.sleep .php#96592

请注意,sleep() 会延迟当前会话的执行,而不仅仅是脚本。考虑以下示例,其中两台计算机从浏览器调用相同的脚本,该脚本除了睡眠之外不执行任何操作。

It is session block your script. not "There is no multithreading in PHP".
session_write_close() before sleep() will solve block your whole script. but may not fit in your problem.

so you had to save the bonus using settimeout of js and AJAX.

from comment of sleep() in php.net:
http://www.php.net/manual/en/function.sleep.php#96592

Notice that sleep() delays execution for the current session, not just the script. Consider the following sample, where two computers invoke the same script from a browser, which doesn't do anything but sleep.

你丑哭了我 2024-08-16 05:38:03

不,不是直接。您需要采取不同的方法,例如记住上次访问的时间戳,并且仅在自那以后经过足够长的时间后才添加积分。

No, not directly. You need to take a different approach, like remembering the timestamp of last visit and only add points if sufficient amount of time has passed since that.

书间行客 2024-08-16 05:38:03

PHP 中没有多线程,因此 sleep() 总是会阻塞整个脚本。

解决这个问题的方法是记录最后一场比赛的时间,只有晚于 45 秒以上才奖励积分。

<?php
session_start();
if (!isset($_SESSION['last_game_time'])
    || (time() - $_SESSION['last_game_time']) > 45) {

    // code to award points here

    $_SESSION['last_game_time'] = time();
}

请记住,如果用户禁用 cookie,他们仍然可能滥用此功能(因此他们将没有会话数据)。因此,如果您真的担心这一点,请在允许他们使用该功能之前检查他们是否启用了 cookie(可能有几个问题涉及到这一点)。

There is no multithreading in PHP, so sleep() is always going to block your whole script.

The way you should solve this is to record the the time of the last game, and only award points if it is more than 45 seconds later than that.

<?php
session_start();
if (!isset($_SESSION['last_game_time'])
    || (time() - $_SESSION['last_game_time']) > 45) {

    // code to award points here

    $_SESSION['last_game_time'] = time();
}

Bear in mind that users could still abuse this if they disable cookies (thus they will have no session data). So if that really worries you, check that they have cookies enabled before allowing them to use the feature (there are probably several questions that cover this).

烈酒灼喉 2024-08-16 05:38:03

不要阻止脚本,而是在会话中保存当前时间,不添加点并让页面呈现。然后,在以后的页面视图中,如果您发现会话中保存的时间超过 45 秒,请添加点,将它们存储在您需要的任何地方,并清除时间。

Instead of blocking the script, save the current time in the session, don't add the points and let the page render. Then on later page views if you see that the saved time in session is older than 45 seconds, add the points, store them wherever you need, and clear the time.

成熟的代价 2024-08-16 05:38:03

您不能,但您可以将此代码设为 JavaScript 代码,并使用 AJAX 保存奖金。

You cannot, but you can make this code a javascript one, and save the bonus using AJAX.

诠释孤独 2024-08-16 05:38:03

问题是 session_start();

尝试

if ($_SESSION['lastgame'] != $gameid) {
    session_write_close();
    sleep(45);
    session_start();
    $points = $points + $game_points;
    $_SESSION['lastgame'] = $gameid;
}

Problem is session_start();

Try

if ($_SESSION['lastgame'] != $gameid) {
    session_write_close();
    sleep(45);
    session_start();
    $points = $points + $game_points;
    $_SESSION['lastgame'] = $gameid;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文