Php 24小时限制

  • 本文关键字:24小时 Php php
  • 更新时间 :
  • 英文 :


我正在为一个名为Ragnarok online的在线游戏使用此代码。我正在制作一个名为vote for points的脚本,但我想在用户ip与数据库上的ip匹配时添加24小时的时间限制。

我的代码:

mysql_connect($Host, $User, $Pswd) or die(mysql_error());
mysql_select_db ($Data) or die ("Bla Bla.");
$Vote = mysql_query("SELECT `last_ip` FROM `vote_por_pontos` WHERE `account_id` = '{$Array['account_id']}'");
$Vote_Result = mysql_fetch_assoc($Vote);
$Vote_Time = mysql_query("SELECT `time` FROM `vote_por_pontos` WHERE `account_id` = '{$Array['account_id']}'");
$Vote_Time_Result = mysql_fetch_assoc($Vote);
if($Vote_Result['last_ip'] == "") {
    mysql_query("INSERT INTO `vote_por_pontos` (`account_id`, `pontos`, `time`, `last_ip`) VALUES ('{$Array['account_id']}', '1', '".time()."', '$_SERVER[REMOTE_ADDR]')");
    echo "Bla bla";
    exit;
}
if($Vote_Result['last_ip'] == $_SERVER['REMOTE_ADDR']) {
   if($Vote_Time_Result['time'] > (???)) {
}

字段time返回epoch时间。

if($Vote_Result['last_ip'] != $_SERVER['REMOTE_ADDR']) {
    mysql_query("UPDATE `vote_por_pontos` SET `pontos` = `pontos` + 1, `last_ip` = '$_SERVER[REMOTE_ADDR]' WHERE `account_id` = '{$Array['account_id']}'");
    echo "Bla bla";
    exit;
}

我已经更新了您的代码以使用mysqli,并且我已经编写了它,因此它将对数据库进行一次查询,以查看ip地址在过去24小时内是否对account_id进行了投票。如果他们还没有做点什么:

<?php
$mysqli = new mysqli($Host, $User, $Pswd, $Data);
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %sn", mysqli_connect_error());
    exit();
}
$stmt = $mysqli->prepare('SELECT 1 FROM vote_por_pontos WHERE account_id = ? AND lastip = ? AND `time` >= ?');
$account_id = $Array['account_id'];
$ip_address = $_SERVER['REMOTE_ADDR'];
$time       = time() - (24 * 60 * 60); // 24 hours ago
$stmt->bind_param('d', $account_id);
$stmt->bind_param('s', $ip_address);
$stmt->bind_param('d', $time);
$stmt->execute();
$stmt->bind_result($already_voted);
$stmt->fetch();
if (!$already_voted) {
    // This ip address hasn't voted on that account in the last 24 hours
    // TODO something here
}
$stmt->close();
/* close connection */
$mysqli->close();
?>

最新更新