同一ip在一小时内不能提交多次
我正在尝试创建一个网络调查问卷,但我不希望同一个人使用相同的 ip 在一个小时内多次提交它,我正在使用 php,我认为我需要使用 $_SERVER['REMOTE_ADDR']
获取客户端ip并存储在会话或数据库中,然后用它来比较新ip,我不确定它是否正确,也不知道如何要在 php 中准确实现这一点,任何人都可以帮助我,提前致谢!
I am trying to create a web survey questionaire, but I don't want same people using same ip to submit it more than once within an hour time, I am using php, I assume I need to use$_SERVER['REMOTE_ADDR']
to get the client ip and store in the session or database and then use it to compare the new ip, I am not sure if it is right and don't know how to exact implement this in php, can anyone help me with it, thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
提交调查时:
但是,您可能会发现这不是一个好的要求 - 在许多有效情况下,多个用户可能使用相同的 IP 地址(例如,学生住宿)。您可能会通过施加此限制来阻止有效提交。
编辑
以下是如何使用 cookie 执行此操作的基本概述(考虑下面讨论的限制)。
我们的 cookie 系统基于一对值。
ckv_1
将保存上次提交调查的时间戳。ckv_2
将保存基于时间戳和盐的哈希值,以防止人们篡改 cookie。显然,如果两个 cookie 都被删除,我们将无法检测到它,但至少这提供了某种验证:When survey is submitted:
However, you may find that this is not a good requirement - there are many valid situations where multiple users may use the same IP address (such as, for example, student accomodation). You may be preventing valid submissions by imposing this limit.
EDIT
Here is a basic outline of how you might do this with cookies (taking into account the limitations discussed below).
Our cookie system works on a pair of values.
ckv_1
will hold the timestamp at which the last survey was submitted.ckv_2
will hold a hash based on the timestamp and a salt, in an effort to stop people from screwing with the cookies. Obviously, if both cookies are deleted, we won't be able to detect it, but at least this provides some sort of validation:使用数据库来存储投票的 IP 和时间戳,然后在记录投票时(或者更好;在显示调查时告诉用户他已经投票了)检查数据库是否用户的 IP ($_SERVER['REMOTE_ADDR'] ) 已在数据库中,并且时间戳小于一小时。如果是,则不允许他投票,否则就允许他投票。
Use a database to store IPs and timestamps of votes, and then when recording the vote (or better yet; when displaying the survey so you tell the user that he already voted) check the database if user's IP ($_SERVER['REMOTE_ADDR']) is already in the DB and if the timestamp is younger than one hour. If it is don't allow him to vote, otherwise do.
您可以将检查包装在一个类中,然后在您的操作需要以下功能时使用它:
You can wrap your checks in a class and then use it when your action requires the functionality:
REMOTE_ADDR
确实可以为您提供 IP 地址。但是:因此,这里是对
REMOTE_ADDR
的更准确描述:作为完整 TCP 会话地址(服务器、服务器端口、远程地址、远程端口)的一部分的地址,可让您发送包含该地址一部分的数据包。所说的会议。它可能是也可能不是实际客户的地址(通常不是),可能与请求之间匹配,也可能不匹配,并且可能会也可能不会被许多其他人共享。REMOTE_ADDR
does indeed get you an IP address. But:So, here is a more accurate description of
REMOTE_ADDR
: An address that, as part of the full TCP session address (server, server port, remote address, remote port) lets you send a packet that is part of said session. It may or may not be the address of the actual client (usually isn't), may or may not match from request-to-request, and may or may not be shared by numerous other people.将
$_SERVER['REMOTE_ADDR']
及其上次提交调查的时间戳存储在数据库表中。该表可能有两列,例如IPAddress(varchar 100)、TimeStamp(int)
。并在 php 代码中Store the
$_SERVER['REMOTE_ADDR']
in database table with the time stamp it last submitted the survey. The table may have two columns likeIPAddress(varchar 100), TimeStamp(int)
. and in php code