Hello all,

I would like to set a time after 2 requests that someone can make a request again does anyone have an example for this.

thanks in advance

Dani AI

Generated

A simple, reliable way to enforce "allow 2 requests then wait X seconds" with MySQL + PHP is to keep a tiny per-user rate row (count + window start) and use a transaction with row locking to make changes atomic. This avoids scanning a large request log and is easy to reason about.

Create table (InnoDB):

CREATE TABLE request_rate (
  user_key VARCHAR(128) NOT NULL PRIMARY KEY,
  req_count TINYINT UNSIGNED NOT NULL DEFAULT 0,
  window_start DATETIME NOT NULL,
  INDEX (window_start)
) ENGINE=InnoDB;

Basic request-check flow (PDO example):

/* $pdo = PDO instance; $userKey = user id / API key / IP; $max = 2; $wait = 60; */

$pdo->beginTransaction();
$stmt = $pdo->prepare('SELECT req_count, window_start FROM request_rate WHERE user_key = ? FOR UPDATE');
$stmt->execute([$userKey]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$now = new DateTime();

if (!$row) {
  $insert = $pdo->prepare('INSERT INTO request_rate (user_key, req_count, window_start) VALUES (?, 1, ?)');
  $insert->execute([$userKey, $now->format('Y-m-d H:i:s')]);
  $pdo->commit();
  // allow
} else {
  $elapsed = $now->getTimestamp() - (new DateTime($row['window_start']))->getTimestamp();
  if ($elapsed >= $wait) {
    $update = $pdo->prepare('UPDATE request_rate SET req_count = 1, window_start = ? WHERE user_key = ?');
    $update->execute([$now->format('Y-m-d H:i:s'), $userKey]);
    $pdo->commit();
    // allow
  } elseif ($row['req_count'] < $max) {
    $update = $pdo->prepare('UPDATE request_rate SET req_count = req_count + 1 WHERE user_key = ?');
    $update->execute([$userKey]);
    $pdo->commit();
    // allow
  } else {
    $pdo->rollBack();
    // deny — still inside wait window
  }
}

Notes and caveats: use a stable identifier (user id or API key). IP-based keys can block many real users behind NAT. Keep transactions short and use InnoDB so FOR UPDATE works. Add a cleanup job to remove stale rows older than your retention window. For very high traffic, a memory-backed limiter is preferable; for moderate traffic this DB approach is simple and accurate. This builds on 's flood-control idea but shows a MySQL/PHP-only implementation tailored to 's request.

Recommended Answers

All 2 Replies

Basically what you're looking for is something to manage rate limiting or flood control.

I wrote a little code snippet up that shows the code we are using to manage flood control. We use Redis, a persistent caching mechanism. You can alternatively use Memcached to achieve something very similar.

Another thought, but this will cost you some money if you're not already a Cloudflare subscriber. If you use Cloudflare CDN, they have a pay-per-use frequency capping feature.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.