通过IP地址限制用户对功能的访问 [英] Restrict access to functionality for user by IP address

查看:99
本文介绍了通过IP地址限制用户对功能的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个允许任何用户使用功能的网站,但每个用户只能使用固定的时间.

I have a website that allows a functionality for any user but each user is only allowed to use it a fixed amount of times.

让我更详细些,我们的匿名/访客"用户每24小时只能在数据库中搜索3次.有没有一种方法可以使用IP跟踪用户的尝试次数,将其限制为3次尝试,然后使其在24小时后过期?

Allow me to go into some more detail, our "anonymous/guest" user is allowed to search on the database for entries only 3 times per 24 hours. Is there a way I can use the IP to track the users attempts at this, restricting them after 3 attempts and then get it to expire after 24 hours?

该网站是用PHP构建的,但是无论使用哪种语言提供此功能,我都愿意接受.

The website is built in PHP but whatever language serves this functionality, I am open to it.

这个想法来自一个客户,他们有一个库存商"库存他们的产品,并且他们希望将此信息提供给网站的用户.但是,他不希望竞争对手利用他的系统并削弱"他的库存.因此,限制.如果您可以提出实现此目标的更好方法,那就太好了

This idea comes from a client, they have a list of "stockists" stocking their products and they want to make this information available to users of the website. However, he doesn't want competitors taking advantage of his system and "undercutting" him on his stockists. Hence the restriction. If you can suggest a better way to achieve the same then that'd be amazing

干杯, 丹

推荐答案

您必须使用$ _SERVER全局变量,如下所示:

You have to use the $_SERVER global variable, like this:

if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') {
  // restrict
}

但是您必须考虑kojiro的注释:限制IP地址不是一个好主意.

But You have to consider kojiro's note: it's not a good idea to restrict by IP address.

修改: 具有建设性.

如果您想在较低级别上进行操作,则可以使用Cookie.这些变量存储在用户的浏览器中,如果用户在本地清除cookie,则他/她可以覆盖您的限制.
http://www.php.net/manual/zh/function.setcookie.php

If you want to do it on low level, you can use cookies. These variables stored in the user's browser, if the user clear the cookies locally, he/she can override your restriction.
http://www.php.net/manual/en/function.setcookie.php

更好的解决方案是为此会话使用会话:
http://php.net/manual/en/session.examples.basic.php

The better solution is to use sessions for this filter:
http://php.net/manual/en/session.examples.basic.php

这篇关于通过IP地址限制用户对功能的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆