-4

我有一些代码可以向我的数据库提交一些值,我希望客人能够每 24 小时提交一次。我更想知道如何使用 cookie 来做到这一点,因为 IP 限制会限制 NAT 用户。

它必须在一个单独的 PHP 文件中吗?

这是我当前的代码:

<?php
$con=mysqli_connect("host","user","pass","db_name");
// Check connection
if (mysqli_connect_errno ())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

//post result to db
$result_set = mysqli_query($con,"SELECT points FROM total WHERE id = 1");
$row = mysqli_fetch_assoc($result_set);
$old_total = $row['points'];
$new_total = $old_total + $_REQUEST['total'];
mysqli_query($con,"UPDATE total SET points = $new_total WHERE id = 1");
mysqli_close($con);
?>
4

2 回答 2

1

如果用户删除 cookie 或只是拒绝接受 cookie 怎么办?他可以在您的数据库中发送数以万计的插入。cookie 不是“保护”某些东西的安全方式。同时通过IP+Cookie限制或做一些登录系统。

无论如何,做你想做的,如果没有设置,这里是设置一个cookie代码:

  if(isset($_COOKIE['somenameofmycookie'])) {
    echo 'you can not submit a form!';
  } else {
    setcookie("somenameofmycookie", $info, time()+1111111); //write in $info what do you want and set a proper time to 24 hours, more at php.net
  }
于 2013-03-10T10:53:30.050 回答
0
<?php
function is_submit_allowed ($form_name, $timeout)
{
    session_set_cookie_params ($timeout);
    @session_start ();

    $key = "${form_name}_last_submit_time";
    $now = time ();
    if (!isset ($_SESSION [$key]) || $_SESSION [$key] <= $now - $timeout)
    {
        $_SESSION [$key] = $now;
        return true;
    }
    else return false;
}

if (is_submit_allowed ("vote_form", 24 * 60 * 60))
{
    echo ("Submitting...");
}
else
{
    echo ("You can only submit once every 24 hours");
}
?>
于 2013-03-10T10:54:27.030 回答