我认为您的要求是:
1) 有人使用作为 URL ($_GET) 的一部分传递的记录 ID 访问您的网站。
a) 如果他们在经过一段时间的“时间间隔”后访问,则记录“ID”。
b) 如果他们之前访问过并且“时间间隔”尚未到期,则记录“id”的请求将被忽略。
“时间间隔”记录在名为“已访问”的 COOKIE 中。
您将详细信息记录在数据库中 - 我将 id 存储在 $_SESSION 变量中。仅供演示。
我只展示方法。它并不意味着高效。它被设计为非常容易调试。它报告每个步骤和状态。
我使用 20 秒的“时间间隔”。
我有一个工作示例:viper-7.com?id=9/。注意:$_GET[id] 参数是在'viper-7' URL上传递的!您可以更改它以确保代码有效。它将拒绝您的新 ID,直到 20 秒过去。
代码(已注释):
<?php
session_start(); // Testing only - store last ID in $_SESSION['id']!
define ('VOTE_DELAY', '20'); // seconds
// show 'visited cookie or if missing...
if (isset($_COOKIE['visted'])) {
echo '<pre>';
var_dump($_COOKIE['visited']); // show all cookies currently active
echo '</pre>';
}
else {
echo '<br />No current $_COOKIE[visited] found!';
echo '<br />';
}
// DEBUG - show current ID
echo '<br />Current Session ID: ', !empty($_SESSION['id']) ? $_SESSION['id'] : 'none';
echo '<br />';
// process whether visited in the last interval (20 seconds)
if (isset($_COOKIE['visited'])) {
echo '<br />active Cookie: ', $_COOKIE['visited'],' - will leave now...';
return; // no more voting until cookie expires
}
// no id provided - is error
if (empty($_GET['id'])) {
echo '<br />no valid $_GET[id] provided : will leave - NO Visited Cookie set ';
return; // do nothing
}
$id = $_GET['id']; // store new id in the session rather than the database
echo "<br />New VALID visit: will store cookie for: ", $id, ' in the $_SESSION[id] variable';
setcookie("visited", "$id", time() + VOTE_DELAY); // set the 'visited cookie
$_SESSION['id'] = $id; // store in session rather than the database.
// mysql_query("UPDATE movie SET movie_views = ( movie_views + 1) WHERE id = $gid ");
echo "<br /> new cookie set...";
我不知道你的代码为什么不起作用。
此外,如果客户端删除“已访问”cookie,那么他们可以根据需要随时记录“id”。您需要分析数据库中的记录,以确保“id 的记录”有效。