保存到txt文件的小帮助
你好,所以我只是设置了这个基本的民意调查,我从那里发现的一些东西中启发了自己,这只是一个基本的 ajax 民意调查,将结果发送到文本文件中。
虽然我很想知道,因为我不希望用户简单地大量点击来对结果有利/不利,所以我考虑添加一个新的文本文件,该文件可以简单地保存 IP,每行一个,然后检查它是否已经存在已记录,如果是,则显示结果,如果不是,则显示投票。
我保存结果的代码行是:
<?php
$vote = $_REQUEST['vote'];
$filename = "votes.txt";
$content = file($filename);
$array = explode("-", $content[0]);
$yes = $array[0];
$no = $array[1];
if ($vote == 0)
{
$yes = $yes + 1;
}
if ($vote == 1)
{
$no = $no + 1;
}
$insert = $yes."-".$no;
$fp = fopen($filename,"w");
fputs($fp,$insert);
fclose($fp);
?>
所以我想知道如何检查 IP,就像基本上一样。
我对数据库不感兴趣,即使是安全措施,我对我所拥有的也很满意。
感谢任何帮助!
Hello there so I just setup this basic poll, I inspired myself from something I found out there, and it's just a basic ajax poll that waves the results in a text file.
Although I was wondering, since I do not want the user to simply mass-click to advantage / disadvantage the results, i thought about adding a new text file that could simply save the IP, one on each line, and then checks if it's already logged, if yes, display the results, if not, show the poll.
My lines of code to save the result are:
<?php
$vote = $_REQUEST['vote'];
$filename = "votes.txt";
$content = file($filename);
$array = explode("-", $content[0]);
$yes = $array[0];
$no = $array[1];
if ($vote == 0)
{
$yes = $yes + 1;
}
if ($vote == 1)
{
$no = $no + 1;
}
$insert = $yes."-".$no;
$fp = fopen($filename,"w");
fputs($fp,$insert);
fclose($fp);
?>
So I'd like to know how I could check out the IPs, in the same way it does basically.
And I'm not interested in database, even for security measures, I'm alright with what Ive got.
Thanks to any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了阻止多次投票,我会在用户投票后设置一个 cookie。如果用户重新加载带有投票表格的页面并且有 cookie,您可以只显示结果,或者显示“您已经投票”。信息。请注意,这不会阻止狡猾的人进行双重投票 - 他们所要做的就是删除保存的 cookie,然后他们就可以重新投票。
请记住,IP 可以共享,因此您存储 IP 的想法可能会适得其反 - 共享的面向外部的 IP 上的人将无法投票,因为您的系统将记录来自同一 IP 地址的某人的先前投票。
To stop multiple votes, I'd set a cookie once a user has voted. If the user reloads the page with the voting form on it and has a cookie, you could show just the results, or a "You have already voted." message. Note that this will not stop craftier people from double-voting - all they would have to do is remove the saved cookie, and they could re-vote.
Keep in mind though that IPs can be shared so your idea of storing IPs might backfire - people on a shared external-facing IP won't be able to vote, as your system will have registered a previous vote from someone at the same IP address.
最简单的方法是将数据写入文件
并从文件中读取数据
以获取用户的IP地址
$_SERVER['REMOTE_ADDR']
请参阅
file_put_contents 的 PHP 手册 了解更多信息
和
file_get_contents< /code>
这是示例代码
easiest way is to write data to file is
and to read data from file
To get IP Address of the user
$_SERVER['REMOTE_ADDR']
See php manual for
file_put_contents
for more information andfile_get_contents
Here is sample code
您可以使用
file_get_contents
将文件内容保存到变量中,然后使用strpos
函数检查该变量中是否存在 IP。例如:
You can use
file_get_contents
to save the file's content into a variable and then use thestrpos
function to check if the IP exists in that variable.For example:
将 IP 存储在文本文件中,然后使用 file_get_contents() 和类似函数加载数据/解析时要小心。作为绝对最坏的情况,假设每个可能的 IP 地址都使用您的系统进行投票,您最终会得到一个大小为许多 GB 的文本文件,并且您很快就会超过 PHP 的内存限制。
Be careful with storing IPs in a text file, and then using
file_get_contents()
and similar functions for loading the data/parseing. As an absolute worst case, assuming that every possible IP address used your system to vote, you'd end up with a text file in the many many gigabytes in size, and you'd exceed PHP's memory_limit very quickly.