dtziv24262 2014-11-02 00:59
浏览 172
已采纳

如何使用php停止将重复值写入文本文件

i have a script that keeps reloading every 2 seconds, i made a code to create a txt file for each user IP and write the user name $name inside it. my problem is that everytime my script reloads it will write the $name of the specific IP again with every reload. the code is

 $ip_file = "ips/".$ip.".txt";

    $logip = fopen($ip_file,"a", 1);

    $name = $name."
";

    fwrite($logip, $name);  

    fclose($logip);

    return;

i need some way to verify if the name is already in the $ip_file and if it's there then not to write it again.

the idea behind this is to check if the same IP is used by more than one $name and then create a function to check all the $ip_file files for more than 1 $name and if so ban the violating $ip

thanks in advance

  • 写回答

2条回答 默认 最新

  • duangouhui0446 2014-11-02 01:14
    关注
    $ip_file = "ips/".$ip.".txt";
    $names = file_get_contents($ip_file); //read names into string
    if(false === strpos($names,$name)) { //write name if it's not there already
        file_put_contents($ip_file,"$name
    ",FILE_APPEND);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误