-3

I'm using this code for one online game called Ragnarok Online. I'm making a script called vote for points, but i want to add a 24 hour time restriction when the user ip matches the ip on database.

My code:

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'] > (???)) {
}

Field time returns epoch time.

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;
}
4

1 回答 1

1

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

<?php
$mysqli = new mysqli($Host, $User, $Pswd, $Data);

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", 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();
?>
于 2013-07-02T19:31:27.407 回答