PHP: 等待时间,函数直到 x 分钟后才能执行
PHP: Waiting time so function can't be executed untill x minutes
我正在尝试为一个小游戏制作一个函数,我正在尝试将等待时间设置为 x 分钟,这样脚本就不会在有人想刷新的时候刷新。计划如下:
- 用户通过普通输入类型使用该功能="submit"- 按钮
- 函数被执行
- 设置等待时间 2 分钟
- 如果此人访问包含该脚本的站点,则会显示一条消息 "You have to wait x minutes untill you can redo the action"。 (如果还没有过去2分钟)
- 2 分钟后,此人将能够看到与第一步相同的内容,并重复该动作。
关于我应该在 PHP 和 MySql 中研究哪些功能的任何建议?
我想在 MySql 中有一个 table 和一个名为 "last_executed" 的字段,它将保存执行操作时的时间戳,我认为我需要的是一个函数来检查是否 2分钟过去了。
采纳所有建议。
谢谢。
您所建议的几乎可以肯定是最好的方法。使用PHP time()
函数设置'last_executed',然后比较:
$diff = time() - $last_executed; // $last_executed is the value from the server
if($diff < 120) { // 2 minutes in seconds
echo 'No can do';
}
else {
echo 'Go ahead';
// Set the last_executed again in the database
}
获取按下按钮的时间。说成 $time_pressed
.
因此在 php 中:$time_btn_pressed = (round(microtime(true), 2)*100)
因此,对于任何操作,进一步获取当前时间并进行比较:
$currentTime = (round(microtime(true), 2)*100) ;
if($currentTime - $time_btn_pressed >= 120){
}
所以函数将某些内容存储到 MySQL 中,对吗?保存时为什么不把最后提交条目的时间戳放在行中呢?然后检查并比较:
// you need to check the database and get $timeFromDB from the thing that function is going to store
$lastStored = strtotime ($timeFromDB);
// get the current time
$currentTime = time ();
// figure out how much time has passed since the thing was last stored
$deltaTime = $currentTime - $lastStored;
// 120 seconds = 2 minutes
if ($deltaTime > 120) {
// call the function
} else {
// tell the user they have to wait $deltaTime seconds
}
您可能希望存储表单在会话中最后提交的时间,然后在返回第一页时呈现表单,或者根据是否设置该时间呈现 "please wait" 消息在会话中,如果是所需时间已过。
只需将用户最后 activity 设置为
$waiting = new letUserWait();
$waiting->setConnection()->setActivityTime();
并检查他是否再次被允许
$allowed = new letUserWait();
$allowed->setConnection()->isUserAllowed();
如果你愿意,你可以在没有 Carbon 的情况下完成此操作,但我建议添加它,这样更方便。如果用户在过去 2 分钟内没有 activity,$allowed
将是 true
。
<?php
namespace waitingTime;
use Carbon;
use PDO;
class letUserWait {
protected $waitingTime;
protected $dbconnection;
public $UserAllowed;
public function setConnection() {
try {
$this->dbconnection = new PDO("mysql:host=".DB_HOST.";dbname=".DATABASE, DB_USERNAME, DB_PASSWORD);
} catch (PDOException $e) {
die($e->getMessage());
}
return $this;
}
public function setActivityTime() {
$stmt = $this->dbconnection->prepare('INSERT INTO user_waiting_time (user_id, timestart) VALUES (':user_id',NOW());
$stmt->execute([':user_id' => $_SESSION['user_id']]);
return true;
}
public function isUserAllowed() {
$stmt = $this->dbconnection->prepare("SELECT user_id FROM user_waiting_time WHERE timestart BETWEEN ".Carbon::now()->subMinutes(2)->toDateTimeString." AND ".Carbon::now());
$stmt->execute();
$rows = $sth->fetch(PDO::FETCH_NUM);
$this->UserAllowed = true;
if ($rows[0] > 0) {
$this->UserAllowed = false;
}
return $this->UserAllowed;
}
}
我正在尝试为一个小游戏制作一个函数,我正在尝试将等待时间设置为 x 分钟,这样脚本就不会在有人想刷新的时候刷新。计划如下:
- 用户通过普通输入类型使用该功能="submit"- 按钮
- 函数被执行
- 设置等待时间 2 分钟
- 如果此人访问包含该脚本的站点,则会显示一条消息 "You have to wait x minutes untill you can redo the action"。 (如果还没有过去2分钟)
- 2 分钟后,此人将能够看到与第一步相同的内容,并重复该动作。
关于我应该在 PHP 和 MySql 中研究哪些功能的任何建议? 我想在 MySql 中有一个 table 和一个名为 "last_executed" 的字段,它将保存执行操作时的时间戳,我认为我需要的是一个函数来检查是否 2分钟过去了。
采纳所有建议。
谢谢。
您所建议的几乎可以肯定是最好的方法。使用PHP time()
函数设置'last_executed',然后比较:
$diff = time() - $last_executed; // $last_executed is the value from the server
if($diff < 120) { // 2 minutes in seconds
echo 'No can do';
}
else {
echo 'Go ahead';
// Set the last_executed again in the database
}
获取按下按钮的时间。说成 $time_pressed
.
因此在 php 中:$time_btn_pressed = (round(microtime(true), 2)*100)
因此,对于任何操作,进一步获取当前时间并进行比较:
$currentTime = (round(microtime(true), 2)*100) ;
if($currentTime - $time_btn_pressed >= 120){
}
所以函数将某些内容存储到 MySQL 中,对吗?保存时为什么不把最后提交条目的时间戳放在行中呢?然后检查并比较:
// you need to check the database and get $timeFromDB from the thing that function is going to store
$lastStored = strtotime ($timeFromDB);
// get the current time
$currentTime = time ();
// figure out how much time has passed since the thing was last stored
$deltaTime = $currentTime - $lastStored;
// 120 seconds = 2 minutes
if ($deltaTime > 120) {
// call the function
} else {
// tell the user they have to wait $deltaTime seconds
}
您可能希望存储表单在会话中最后提交的时间,然后在返回第一页时呈现表单,或者根据是否设置该时间呈现 "please wait" 消息在会话中,如果是所需时间已过。
只需将用户最后 activity 设置为
$waiting = new letUserWait();
$waiting->setConnection()->setActivityTime();
并检查他是否再次被允许
$allowed = new letUserWait();
$allowed->setConnection()->isUserAllowed();
如果你愿意,你可以在没有 Carbon 的情况下完成此操作,但我建议添加它,这样更方便。如果用户在过去 2 分钟内没有 activity,$allowed
将是 true
。
<?php
namespace waitingTime;
use Carbon;
use PDO;
class letUserWait {
protected $waitingTime;
protected $dbconnection;
public $UserAllowed;
public function setConnection() {
try {
$this->dbconnection = new PDO("mysql:host=".DB_HOST.";dbname=".DATABASE, DB_USERNAME, DB_PASSWORD);
} catch (PDOException $e) {
die($e->getMessage());
}
return $this;
}
public function setActivityTime() {
$stmt = $this->dbconnection->prepare('INSERT INTO user_waiting_time (user_id, timestart) VALUES (':user_id',NOW());
$stmt->execute([':user_id' => $_SESSION['user_id']]);
return true;
}
public function isUserAllowed() {
$stmt = $this->dbconnection->prepare("SELECT user_id FROM user_waiting_time WHERE timestart BETWEEN ".Carbon::now()->subMinutes(2)->toDateTimeString." AND ".Carbon::now());
$stmt->execute();
$rows = $sth->fetch(PDO::FETCH_NUM);
$this->UserAllowed = true;
if ($rows[0] > 0) {
$this->UserAllowed = false;
}
return $this->UserAllowed;
}
}