doujiu3095 2018-10-05 13:13
浏览 103
已采纳

(PHP)存储点击的IP地址

After almost 3 days of troubleshooting I gotta ask for advice.

A have a small imageboard with 4 images and 4 'like' buttons. Earlier I made it so the number of clicks with each button stores in a .txt file. Now I basically need to make it so a person can press a certain button only once.

This is ip.txt. The number to the left is button ID, to the right is IP of the person that clicked that button.

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. Now for the last few days it's been doing anything except that!

My current code with isset. That sees the first IP but doesn't add the second:

$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
";
                        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
";
                            file_put_contents($file2, $lines2); 
                        } else {
                            echo "lul"; 
                        }
                    } 
                } 
            }
        }
    }
}
fclose($fh2);

I also tried this with in_array function:

$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
";
        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.

This is brutal. What am I doing wrong and is there an easier way?

  • 写回答

2条回答 默认 最新

  • dowm41315 2018-10-05 13:45
    关注

    Use MySQL database to accomplish this.
    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.

    I do not recommend using just IP tho as some ISP gives dynamic IP that changes the IP (Public IP) address every few seconds.
    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!

    Information about MySQL Insert and Select are everywhere on Google.
    Here's one : https://www.w3schools.com/sql/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器