简体   繁体   English

(PHP)存储点击的IP地址

[英](PHP) storing IP address of clicks

After almost 3 days of troubleshooting I gotta ask for advice. 经过近三天的故障排除,我必须寻求建议。

A have a small imageboard with 4 images and 4 'like' buttons. 一个具有4个图像和4个“喜欢”按钮的小图像板。 Earlier I made it so the number of clicks with each button stores in a .txt file. 之前,我做到了,因此每个按钮的点击次数都存储在.txt文件中。 Now I basically need to make it so a person can press a certain button only once . 现在我基本上需要这样做,以便一个人只能按下某个按钮一次

This is ip.txt . 这是ip.txt The number to the left is button ID, to the right is IP of the person that clicked that button. 左边的数字是按钮ID,右边的数字是单击该按钮的人的IP。

click-001||127.0.0.1
click-002||

This is very simple. 这很简单。 I need to make sure it stores ip when I click on my PC, then stores another IP when I click on my pad - and stops whatever I do next. 我需要确保在我单击PC时它存储ip,然后在我单击键盘上时存储另一个IP- 并停止下一步的操作。 Now for the last few days it's been doing anything except that! 现在,最近几天它一直在做任何事情!

My current code with isset . 我当前的代码与isset That sees the first IP but doesn't add the second: 可以看到第一个IP,但不添加第二个:

$file2 = 'ip.txt'; // path to text file that stores counts
$fh2 = fopen($file2, 'r+');
$ip_addr = $_SERVER['REMOTE_ADDR'];
$lines2 = '';
while(!feof($fh2)) {
    $line2 = trim(fgets($fh2));
    if ($line2) {
        $line2 = explode('||', $line2);
        if(isset($line2[0], $line2[1])) {
            $item2 = trim($line2[0]);
            if(!empty($item2)) {
                if($item2 == $id) { 
                    if(empty($line2[1])) {
                        $lines2 .= "$item2||$ip_addr\r\n";
                        file_put_contents($file2, $lines2);
                    } else {
                        // this is where it always fails
                        if (!isset($ip_addr)) {                             $ip_all = $line2[1] . " " . $ip_addr;
                            $lines2 .= "$item2||$ip_all\r\n";
                            file_put_contents($file2, $lines2); 
                        } else {
                            echo "lul"; 
                        }
                    } 
                } 
            }
        }
    }
}
fclose($fh2);

I also tried this with in_array function: 我也尝试了in_array函数:

$ip_all = array($line2[1]); 
    if (!in_array($ip_addr, $ip_all)) {
        array_push($ip_all, ',' , $ip_addr);            
        $ip_fin = implode($ip_all);
        $lines2 .= "$item2||$ip_fin\r\n";
        file_put_contents($file2, $lines2); 

^ This one also sees the first IP and adds the second , but then fails to find whether the IP is already there and just keeps adding copies when I click. ^这个还可以看到第一个IP 并添加第二个 IP,但是随后找不到IP是否已经存在,并且在单击时一直保持添加副本。

This is brutal. 这是残酷的。 What am I doing wrong and is there an easier way? 我在做什么错,有没有更简单的方法?

Use MySQL database to accomplish this. 使用MySQL数据库来完成此任务。
Using a text file is super inefficient and can cause conflicts when multiple users liked at the same time! 使用文本文件效率极低,并且当多个用户同时喜欢时会导致冲突!

Insert the IP to database everytime a user clicked the 'Like' button and then use a select query to determine if this IP has liked the picture before. 每次用户单击“赞”按钮时,将IP插入数据库,然后使用选择查询确定该IP以前是否喜欢该图片。

I do not recommend using just IP tho as some ISP gives dynamic IP that changes the IP (Public IP) address every few seconds. 我不建议仅使用IP,因为某些ISP会提供动态IP,该IP每隔几秒钟会更改IP(公共IP)地址。
Use cookies to store a unique cookie for a user (if they are not logged in) or just ask the user to login first before voting! 使用Cookie为用户存储唯一的Cookie(如果他们尚未登录),或者只是要求用户先登录才能投票!

Information about MySQL Insert and Select are everywhere on Google. 有关MySQL Insert和Select的信息在Google上无处不在。
Here's one : https://www.w3schools.com/sql/ 这是一个: https : //www.w3schools.com/sql/

best method: use a database or use XML (XML have very useful library) 最佳方法:使用数据库或使用XML(XML有非常有用的库)


text file method: 文本文件方法:

get file and edit... 获取文件并编辑...

$id = "...";
$ip="...";
$file2 = file('ip.txt'); 


$file2 = array_map(
  function($current_line) use ($id,$ip) {
    $current_line = explode('||', $current_line);
    if($current_line[0] == $id){
        $current_line[]=$ip;
    }
    return join("||",$current_line);
  },$file2
);

file_put_contents('ip.txt', implode('\n', $file2));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM