PHP/MYSQL IP 检查/记录器

发布于 2024-12-29 17:55:41 字数 142 浏览 0 评论 0原文

好的,我有一个我的世界服务器,我正在为其制作一个投票脚本。

我需要一种方法来检查 mySQL 中的用户 ip。 如果不存在,则会将该 IP 放入数据库中,并在数据库中 24 小时后将其删除。

如果有人可以帮助我做到这一点,那就太好了,谢谢

Ok, ive got a minecraft server and im making a voting script for it.

I need a way to check the users ip in a mySQL.
If it doesn't exist it will put the ip in the database, and it will remove it after 24 hours in the database.

If anyone could help me do this, it would be great, thanks

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

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

发布评论

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

评论(2

殊姿 2025-01-05 17:55:41

网上有很多关于如何实现这一点的教程,因此我不会给你代码。只需一些提示即可帮助您入门。

在 PHP 中,您可以从 $_SERVER 超全局中获取用户的 ip 地址

$ip = $_SERVER['REMOTE_ADDR'];

来检查它是否存在于您的表中

$query = sprintf("SELECT COUNT(*) FROM yourtable WHERE `ip` = '%s'", mysql_real_escape_string($ip));

如果不存在,则使用 INSERT 命令存储它带有时间戳。

至于 24 小时后删除,您可以设置一个 cron 作业,每小时左右运行一次表,并删除所有已过期的条目。但是,不删除这些条目会更容易。相反,当用户投票时,只需检查他上次投票的时间戳即可。如果距离上次投票已经过去了 24 小时,则只需更新时间戳即可。

基本上,工作流程是:

1. Get users's IP address.
2. Does this IP exist in table? 
  2a. If no, let user vote, and enter his ip in the table along with the current time in the timestamp column
  2b. If yes, get the timestamp of his last vote. Has it been 24 hours since his last vote?
    2b.a. If yes, let user vote and update timestamp column with current time.
    2b.b. If no, do not let user vote / reject his vote. Leave timestamp column unchanged.

There are plenty of tutorials online on how to achieve this, thus I wont give you the code. Just a few hints to get you started.

In PHP, you can get the user's ip address from the $_SERVER superglobal

$ip = $_SERVER['REMOTE_ADDR'];

To check if it exists in your table

$query = sprintf("SELECT COUNT(*) FROM yourtable WHERE `ip` = '%s'", mysql_real_escape_string($ip));

If it does not exist, store it using the INSERT command along with a timestamp.

As for removing after 24 hours, you can set up a cron job that runs through the table every hour or so and removes all entries that have expired. However, it would be easier to not remove the entries. Instead, when a user votes, just check the timestamp of his last vote. If 24 hours have passed since the last vote, just update the timestamp.

Basically, the workflow would be:

1. Get users's IP address.
2. Does this IP exist in table? 
  2a. If no, let user vote, and enter his ip in the table along with the current time in the timestamp column
  2b. If yes, get the timestamp of his last vote. Has it been 24 hours since his last vote?
    2b.a. If yes, let user vote and update timestamp column with current time.
    2b.b. If no, do not let user vote / reject his vote. Leave timestamp column unchanged.
九公里浅绿 2025-01-05 17:55:41

最简单的方法是执行以下

$ip = $_SERVER['REMOTE_ADDR'];
// Run cleanup for given IP
$result = mysql_query("SELECT * FROM table 
             WHERE `ip`='$ip' AND `timestamp` > NOW() - 24*3600");  
if (mysql_num_rows($result)==0) 
// there is no record within the last 24h, let insert a new one
{  
 mysql_query("INSERT INTO table SET `ip`='$ip', `timestamp` = NOW()");  
 echo "Thank you for your vote!";
}
else echo "You may vote only once in 24 hours";
// run total cleanup query from time to time
if (rand(1,5)<2) mysql_query("DELETE FROM table 
                             WHERE `timestamp` < NOW() - 24*3600");  

操作:如果您有不同的投票,则每个投票都应该有不同的唯一 ID。例如,MySQL 表之前有两列 iptimestamp,应该再多一列 voteid。在这种情况下,上面的代码将更改为

// voteid should be somehow set to the id of the voting. 
// for example by GET request
$voteid = intval($_GET['voteid']);
$ip = $_SERVER['REMOTE_ADDR'];
// Run cleanup for given IP
$result = mysql_query("SELECT * FROM table 
   WHERE `ip`='$ip' AND `voteid`='$voteid` AND `timestamp` > NOW() - 24*3600");  
if (mysql_num_rows($result)==0) 
// there is no record within the last 24h, let insert a new one
{  
   mysql_query("INSERT INTO table SET `ip`='$ip', 
                `voteid`='$voteid', `timestamp` = NOW()");  
   echo "Thank you for your vote!";
}
  else echo "You may vote only once in 24 hours";
  // run total cleanup query from time to time
 if (rand(1,5)<2) mysql_query("DELETE FROM table 
                                 WHERE `timestamp` < NOW() - 24*3600");  

The easiest way is to do the following

$ip = $_SERVER['REMOTE_ADDR'];
// Run cleanup for given IP
$result = mysql_query("SELECT * FROM table 
             WHERE `ip`='$ip' AND `timestamp` > NOW() - 24*3600");  
if (mysql_num_rows($result)==0) 
// there is no record within the last 24h, let insert a new one
{  
 mysql_query("INSERT INTO table SET `ip`='$ip', `timestamp` = NOW()");  
 echo "Thank you for your vote!";
}
else echo "You may vote only once in 24 hours";
// run total cleanup query from time to time
if (rand(1,5)<2) mysql_query("DELETE FROM table 
                             WHERE `timestamp` < NOW() - 24*3600");  

ps: If you have different votings, each of them should have different unique ID. and the MySQL table, which had before two columns ip and timestamp, should have one more column voteid, for example. In that case the code above will change to

// voteid should be somehow set to the id of the voting. 
// for example by GET request
$voteid = intval($_GET['voteid']);
$ip = $_SERVER['REMOTE_ADDR'];
// Run cleanup for given IP
$result = mysql_query("SELECT * FROM table 
   WHERE `ip`='$ip' AND `voteid`='$voteid` AND `timestamp` > NOW() - 24*3600");  
if (mysql_num_rows($result)==0) 
// there is no record within the last 24h, let insert a new one
{  
   mysql_query("INSERT INTO table SET `ip`='$ip', 
                `voteid`='$voteid', `timestamp` = NOW()");  
   echo "Thank you for your vote!";
}
  else echo "You may vote only once in 24 hours";
  // run total cleanup query from time to time
 if (rand(1,5)<2) mysql_query("DELETE FROM table 
                                 WHERE `timestamp` < NOW() - 24*3600");  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文