PHP - 24 小时阻止 IP

发布于 2024-12-21 09:13:05 字数 221 浏览 2 评论 0原文

我有一个 PHP 文件,我想收集人们的 IP,然后如果他们的 IP 在过去 24 小时内运行过该文件,则阻止 PHP 文件继续运行。我尝试过使用 cookie,但它一直给我“无法更改标头”错误。另外,人们可以清除他们的cookie。基本上,它会保留运行 php 文件的每个人的 IP,然后如果他们尝试在 24 小时内访问它,它会“回显“您可以在 24 小时内再次访问此文件”并且不会运行整个文件。然后他们可以24小时后再做一次。

I have a PHP file that I want to collect people's IP's then prevent the PHP file from continuing if their IP has run the file within the last 24 hours. I've tried with cookies, but it kept giving me "can't change header" errors. Plus, people could just clear their cookies. Basically, it keeps the IP's of everyone who runs the php file, then if they try to access it within 24 hours it does "echo "You can access this again in 24 hours" and doesn't run the whole file. Then they can do it again after 24 hours.

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

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

发布评论

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

评论(5

心不设防 2024-12-28 09:13:05

每次查看页面时,都会在删除超过 24 小时的条目后检查 IP 地址是否在数据库表中。

// Purge records
mysql_query("DELETE FROM ip_table WHERE access_date < DATE_SUB(CURDATE(), INTERVAL 24 HOUR)");

$ip = $_SERVER['REMOTE_ADDR'];
$result = mysql_query("SELECT ip FROM ip_table WHERE ip = '$ip'");
if($result){
  die("You can access this again in 24 hours");
} 
else {
  $result = mysql_query("INSERT INTO ip_table (ip, access_date) VALUES ('$ip', NOW())");
}

但是,这将阻止所有使用共享连接的用户。最好要求登录,然后阻止每个用户的访问。

Every time the page is viewed check if the ip address is in a database table after removing entries that are over 24 hours old

// Purge records
mysql_query("DELETE FROM ip_table WHERE access_date < DATE_SUB(CURDATE(), INTERVAL 24 HOUR)");

$ip = $_SERVER['REMOTE_ADDR'];
$result = mysql_query("SELECT ip FROM ip_table WHERE ip = '$ip'");
if($result){
  die("You can access this again in 24 hours");
} 
else {
  $result = mysql_query("INSERT INTO ip_table (ip, access_date) VALUES ('$ip', NOW())");
}

However, this will block all users using a shared connection. It is better to require a login, then block access per user.

回忆躺在深渊里 2024-12-28 09:13:05

我认为对你来说,拥有一个存储每个 ip 的最后访问时间的表会更容易,其结构如下:

Access

 - id          - int
 - ip_addr     - int
 - last_access - datetime

$_SERVER['SERVER_ADDR'] 中的 IP 地址转换为带有 inet_pton() 的整数值,并进行简单的选择该数据库表

I think it would be much easier for you to have a table that stores the last access time for each ip, with a structure like:

Access

 - id          - int
 - ip_addr     - int
 - last_access - datetime

you transform from IP address in $_SERVER['SERVER_ADDR'] to an integer value with inet_pton(), and do a simple select in that DB table

怪我鬧 2024-12-28 09:13:05

我可能经常看到的方法:

IP 禁止

优点:

  • 将阻止使用该 IP 地址的所有浏览器
  • 相当容易设置

缺点:

  • 可能会收到大量误报NAT 后面的人等
  • 用户可以移动到另一台计算机或以其他方式更改 IP(代理)

Cookie

优点:

  • 您拥有其报告的浏览器的身份
  • 易于设置和部署

缺点:< /strong>

  • 非常容易被破解(删除 cookie)

用户登录

优点:

  • 您知道哪个帐户正在下载以及何时
  • 下载 很难通过帐户规避

缺点:

  • 可能如果新帐户易于设置,则很容易规避

注意,$_SERVER 是持有IP 地址和有关请求标头的其他信息。

Approaches I can possibly see working more often than not:

IP Banning

Pros:

  • Will block all browsers with that IP address
  • Fairly easy to setup

Cons:

  • May get lots of false positives for people behind NATs, etc.
  • User could move to another computer or otherwise change IP (proxy)

Cookies

Pros:

  • You have an identity for the browser that it's reporting
  • Easy to setup and deploy

Cons:

  • Very easy to defeat (delete the cookie)

User login

Pros:

  • You know which account is downloading and when
  • Hard to circumvent by account

Cons:

  • Could be easily circumvented if new accounts are easy to setup

Note, $_SERVER is what holds the IP address and other information about the request headers.

腹黑女流氓 2024-12-28 09:13:05

您可以简单地跟踪 IP 及其访问 php 文件的日期,并在 24 小时过去后(如果他们尝试重新访问)更新此日期。

我希望这有帮助。

You can simply track ip's with a date they accessed the php file, and update this date after 24 hours has passed if they try to re-access.

I hope this helps.

满地尘埃落定 2024-12-28 09:13:05

像这样的东西:

$_SESSION['REMOTE_ADDR'] can be stored in a database, compared against
err s/SESSION/SERVER
(rewriting session crap over here, have session on the brain)

更新:-

有用的链接

http://perishablepress.com/press/2007/07/03/how-to-block-ip-addresses-with-php/

http://forums.digitalpoint.com/showthread.php?t=67344

Something like this:

$_SESSION['REMOTE_ADDR'] can be stored in a database, compared against
err s/SESSION/SERVER
(rewriting session crap over here, have session on the brain)

Update :-

Useful links

http://perishablepress.com/press/2007/07/03/how-to-block-ip-addresses-with-php/

http://forums.digitalpoint.com/showthread.php?t=67344

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