如何停止影响我整个PHP代码的PHP sleep()? [英] How can I stop PHP sleep() affecting my whole PHP code?

查看:522
本文介绍了如何停止影响我整个PHP代码的PHP sleep()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在我的街机上,howlingdoggames.com.我有一个积分系统,每次访问带有游戏的页面时,它都会为您提供一个积分.现在,为了减少对此的滥用,我希望做出某种延迟,因此仅在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, now, 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;
      }

   ...

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

But, this just seems to halt my whole website for 45 seconds, because this is in the index.php file, as with alot of the other code to my site.

无论如何,我可以隔离那部分代码,所以它只能使函数起作用

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

$points = $points + $game_points;

等待45秒?

推荐答案

(大多数)PHP中没有多线程.您可以使用Unix系统上的派生进程来做到这一点,但这无关紧要,因为多线程并不是您真正想要的.您只需要这样的简单逻辑:

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'];
  if (!isset($points)) {
    $points = 0;
  }
  $_SESSION['points'] = $points + 10;
  $_SESSION['lastvisit'] = $now;
}

基本上,只有在您上次给出分数之间的增量大于45秒时才给出分数.

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

这篇关于如何停止影响我整个PHP代码的PHP sleep()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆