保存到txt文件的小帮助

发布于 2024-09-27 11:12:00 字数 664 浏览 1 评论 0原文

你好,所以我只是设置了这个基本的民意调查,我从那里发现的一些东西中启发了自己,这只是一个基本的 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 技术交流群。

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

发布评论

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

评论(4

感情洁癖 2024-10-04 11:12:00

为了阻止多次投票,我会在用户投票后设置一个 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.

苏大泽ㄣ 2024-10-04 11:12:00

最简单的方法是将数据写入文件

file_put_contents($filename, $data)

并从文件中读取数据

file_get_contents($filename);

以获取用户的IP地址
$_SERVER['REMOTE_ADDR']

请参阅 file_put_contents 的 PHP 手册 了解更多信息file_get_contents< /code>

这是示例代码

<?php

//  File path
$file = 'votedips.txt';

//  Get User's IP Address
$ip = $_SERVER['REMOTE_ADDR'];

//  Get data from file (if it exists) or initialize to empty string
$votedIps = file_exists($file) ? file_get_contents($file) : '';

//
$ips = explode("\n", $votedIps);
if (array_search($ip, $ips)) {
    //  USER VOTED
} else {
    $ips[] = $ip;
}

//  Write data to file
$data = implode("\n", $ips);
file_put_contents($file, $data);

?>

easiest way is to write data to file is

file_put_contents($filename, $data)

and to read data from file

file_get_contents($filename);

To get IP Address of the user
$_SERVER['REMOTE_ADDR']

See php manual for file_put_contents for more information and file_get_contents

Here is sample code

<?php

//  File path
$file = 'votedips.txt';

//  Get User's IP Address
$ip = $_SERVER['REMOTE_ADDR'];

//  Get data from file (if it exists) or initialize to empty string
$votedIps = file_exists($file) ? file_get_contents($file) : '';

//
$ips = explode("\n", $votedIps);
if (array_search($ip, $ips)) {
    //  USER VOTED
} else {
    $ips[] = $ip;
}

//  Write data to file
$data = implode("\n", $ips);
file_put_contents($file, $data);

?>
笑红尘 2024-10-04 11:12:00

您可以使用 file_get_contents 将文件内容保存到变量中,然后使用 strpos 函数检查该变量中是否存在 IP。
例如:

$ipfile=file_get_contents('ip.txt');
if (strpos($ipfile, $_SERVER['REMOTE_ADDR'])!==FALSE) // show the results
else // show the poll

You can use file_get_contents to save the file's content into a variable and then use the strpos function to check if the IP exists in that variable.
For example:

$ipfile=file_get_contents('ip.txt');
if (strpos($ipfile, $_SERVER['REMOTE_ADDR'])!==FALSE) // show the results
else // show the poll
栀梦 2024-10-04 11:12:00

将 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.

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