添加 24 小时投票系统时遇到问题?

发布于 2024-12-27 23:30:43 字数 1315 浏览 3 评论 0原文

有一个脚本会触发下面的代码,

我想禁止每 24 小时多次执行该脚本。

我希望这个脚本根据数据库中的用户 ID 将上次访问时间存储在表中,然后进行时间计算并将其返回,直到 24 小时到期时间。

有人可以解释如何做到这一点吗?如果有人可以帮助我,我将不胜感激?

<?php
//Input correct values into this section
$dbhost = '888888';
$dbuser = '888888';
$dbpass = '888888';
$dbname = '888888';
$dbtable = 'redeem';
$dbtable2 = 'playersthatvoted';
//------------------------------------
$input = 'diamond 12';
$player = $_POST['Player'];
$time = time();
if(!isset($_COOKIE['24Hourvote'])){
   //---- This is the connection
   $conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ('Error: ' . mysql_error());
   mysql_select_db($dbname);
   $query1 = "INSERT INTO `".$dbname."`.`".$dbtable."` (`player`, `item`) VALUES ('".$player."', '".$input."')";
   $query2 = "INSERT INTO `".$dbname."`.`".$dbtable2."` (`player`, `time`) VALUES ('".$player."', '".$time."')";
   mysql_query($query1);
   mysql_query($query2);
   $query= 'SELECT `player` FROM `playersthatvoted` ASC LIMIT 0, 10 ';
   $result = mysql_query($query);
   mysql_close($conn);
   echo 'Done! Type /redeem in-game to get your diamonds.';
   $ip=@$REMOTE_ADDR;
   setcookie ("24Hourvote",$ip,time()+86400,'/',true,…
} else {
   echo 'You have already voted today! Come back later...'; }
?>

编辑:我可以让它显示用户可以再次投票之前的剩余时间吗?

There is a script that triggers the code below

I want to disallow executing the script more than once per 24 hours.

I wanted this script to store the last visit time in a table against the user id in a database, then do a time calculation and back them out until the 24 hour expiry time.

Can someone explain how to do this? It would be greatly appreciated if someone could help me with this?

<?php
//Input correct values into this section
$dbhost = '888888';
$dbuser = '888888';
$dbpass = '888888';
$dbname = '888888';
$dbtable = 'redeem';
$dbtable2 = 'playersthatvoted';
//------------------------------------
$input = 'diamond 12';
$player = $_POST['Player'];
$time = time();
if(!isset($_COOKIE['24Hourvote'])){
   //---- This is the connection
   $conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ('Error: ' . mysql_error());
   mysql_select_db($dbname);
   $query1 = "INSERT INTO `".$dbname."`.`".$dbtable."` (`player`, `item`) VALUES ('".$player."', '".$input."')";
   $query2 = "INSERT INTO `".$dbname."`.`".$dbtable2."` (`player`, `time`) VALUES ('".$player."', '".$time."')";
   mysql_query($query1);
   mysql_query($query2);
   $query= 'SELECT `player` FROM `playersthatvoted` ASC LIMIT 0, 10 ';
   $result = mysql_query($query);
   mysql_close($conn);
   echo 'Done! Type /redeem in-game to get your diamonds.';
   $ip=@$REMOTE_ADDR;
   setcookie ("24Hourvote",$ip,time()+86400,'/',true,…
} else {
   echo 'You have already voted today! Come back later...'; }
?>

EDIT: and could I make it so that it displays the time left until the user can vote again?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

段念尘 2025-01-03 23:30:43

对我来说,你似乎已经知道你必须做什么:

我希望这个脚本将上次访问时间存储在表中
针对数据库中的用户 ID。然后进行时间计算并且
将其退回,直至 24 小时到期。

所以:

  1. 忘记cookie吧。它存储在客户端并且可以被操作。
  2. 在计票之前检查当前用户的 [lastvisit] 字段。
  3. 如果未设置,则计票并将表中的 [lastvisit] 字段设置为当前日期。
  4. 如果设置,则计算从现在到最后一次投票之间的时间跨度。如果超过 24 小时,请统计投票并将表中的 [lastvisit] 字段设置为当前日期。

请注意:

  • 操纵参数:$_POST['Player'];
  • SQL 注入:VALUES ('".$player."', '".$input."') code>

如果您在执行其中一项任务时遇到问题,请询问具体问题。

To me it looks like you already know what you have to do:

I wanted this script to store the last visit time in a table
against the user id in a database.Then do a time calculation and
back them out until the 24 hour expiry time.

So:

  1. Forget about the cookie. It is stored on client side and can be manipulated.
  2. Before count the vote check the [lastvisit] field of the current user.
  3. If not set count the vote and set the [lastvisit] field in your table to the current date.
  4. If set calculate the time span between now and the last vote. If bigger than 24 hours, count the vote and set the [lastvisit] field in your table to the current date.

Be aware of:

  • Manipulated parameters: $_POST['Player'];
  • SQL injections: VALUES ('".$player."', '".$input."')

If you have problems with one of these tasks then ask about the specific problem.

握住我的手 2025-01-03 23:30:43
<?php
//Input correct values into this section
$dbhost = '888888';
$dbuser = '888888';
$dbpass = '888888';
$dbname = '888888';
$dbtable = 'redeem';
$dbtable2 = 'playersthatvoted';
//------------------------------------
$input = 'diamond 12';
$time = time();
if(!isset($_COOKIE['24Hourvote'])){
       $ip = $_SERVER['REMOTE_ADDR'];
   //---- This is the connection
   $conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ('Error: ' . mysql_error());
   mysql_select_db($dbname);

      // Escape all user entered data always
      $player = mysql_real_escape_string($_POST['Player']);

   // Select time for this player if available
   $query = "SELECT time FROM playersthatvoted WHERE player = '$player' ORDER BY time DESC LIMIT 0, 1";
   $result = mysql_query($query);

   if(mysql_num_rows($result) != 0)
   {
       $row = mysql_fetch_row($result);
       $last_visit = $row[0];
       $vote_allowed_time = $last_visit + 86400; 

       // Allowed to vote
       if($time > $vote_allowed_time)
       {
           // Do whatever else you need to here ...

           setcookie ("24Hourvote",$ip,time()+86400,'/');
       }
       else
       {
           echo 'This player has already voted today! Come back later...';
       }
   }
   else
   {
       $query1 = "INSERT INTO `".$dbname."`.`".$dbtable."` (`player`, `item`) VALUES ('".$player."', '".$input."')";
       $query2 = "INSERT INTO `".$dbname."`.`".$dbtable2."` (`player`, `time`) VALUES ('".$player."', '".$time."')";
       mysql_query($query1);
       mysql_query($query2);
       $query= 'SELECT `player` FROM `playersthatvoted` ASC LIMIT 0, 10 ';
       $result = mysql_query($query);
       mysql_close($conn);
       echo 'Done! Type /redeem in-game to get your diamonds.';

       setcookie ("24Hourvote",$ip,time()+86400,'/');
   }
} else {
   echo 'You have already voted today! Come back later...'; }
?>

注意:永远不要相信用户输入,始终验证并转义数据。

更改:

$player = $_POST['Player'];

为:

$player = mysql_real_escape_string($_POST['Player']);

添加:

 // Select time for this player if available
 $query = "SELECT time FROM playersthatvoted WHERE player = '$player' ORDER BY time DESC LIMIT 0, 1";
 $result = mysql_query($query);


if($result)
   {
       $row = mysql_fetch_row($result);
       $last_visit = $row[0];
       $vote_allowed_time = $last_visit + 86400; 

       // Allowed to vote
       if($time > $vote_allowed_time)
       {
           // Do whatever else you need to here ...

           setcookie ("24Hourvote",$ip,time()+86400,'/');
       }
       else
       {
           echo 'This player has already voted today! Come back later...';
       }
   }
   else
   {
       ...
   }

更新< /strong>

我想强调这样一个事实:就目前情况而言,任何人都可以输入玩家姓名并尝试为其投票,这并不一定意味着点击投票按钮的用户是同一个人。

此外,IP 地址未用于任何目的,使用它进行进一步的权限/安全检查可能是一个主意。

<?php
//Input correct values into this section
$dbhost = '888888';
$dbuser = '888888';
$dbpass = '888888';
$dbname = '888888';
$dbtable = 'redeem';
$dbtable2 = 'playersthatvoted';
//------------------------------------
$input = 'diamond 12';
$time = time();
if(!isset($_COOKIE['24Hourvote'])){
       $ip = $_SERVER['REMOTE_ADDR'];
   //---- This is the connection
   $conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ('Error: ' . mysql_error());
   mysql_select_db($dbname);

      // Escape all user entered data always
      $player = mysql_real_escape_string($_POST['Player']);

   // Select time for this player if available
   $query = "SELECT time FROM playersthatvoted WHERE player = '$player' ORDER BY time DESC LIMIT 0, 1";
   $result = mysql_query($query);

   if(mysql_num_rows($result) != 0)
   {
       $row = mysql_fetch_row($result);
       $last_visit = $row[0];
       $vote_allowed_time = $last_visit + 86400; 

       // Allowed to vote
       if($time > $vote_allowed_time)
       {
           // Do whatever else you need to here ...

           setcookie ("24Hourvote",$ip,time()+86400,'/');
       }
       else
       {
           echo 'This player has already voted today! Come back later...';
       }
   }
   else
   {
       $query1 = "INSERT INTO `".$dbname."`.`".$dbtable."` (`player`, `item`) VALUES ('".$player."', '".$input."')";
       $query2 = "INSERT INTO `".$dbname."`.`".$dbtable2."` (`player`, `time`) VALUES ('".$player."', '".$time."')";
       mysql_query($query1);
       mysql_query($query2);
       $query= 'SELECT `player` FROM `playersthatvoted` ASC LIMIT 0, 10 ';
       $result = mysql_query($query);
       mysql_close($conn);
       echo 'Done! Type /redeem in-game to get your diamonds.';

       setcookie ("24Hourvote",$ip,time()+86400,'/');
   }
} else {
   echo 'You have already voted today! Come back later...'; }
?>

Note: Never trust the user input, always validate and escape the data.

Changed:

$player = $_POST['Player'];

to:

$player = mysql_real_escape_string($_POST['Player']);

Added:

 // Select time for this player if available
 $query = "SELECT time FROM playersthatvoted WHERE player = '$player' ORDER BY time DESC LIMIT 0, 1";
 $result = mysql_query($query);


if($result)
   {
       $row = mysql_fetch_row($result);
       $last_visit = $row[0];
       $vote_allowed_time = $last_visit + 86400; 

       // Allowed to vote
       if($time > $vote_allowed_time)
       {
           // Do whatever else you need to here ...

           setcookie ("24Hourvote",$ip,time()+86400,'/');
       }
       else
       {
           echo 'This player has already voted today! Come back later...';
       }
   }
   else
   {
       ...
   }

UPDATE

I would like to highlight the fact that as it stands anyone can enter the player name and try to vote for it and that does not necessarily mean the same user who clicks the vote button.

Additionally the IP address is not being used for any purposes, it may be an idea to use this for further permission/security checks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文