2

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."\n";

    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

0

2 Answers 2

3
$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\n",FILE_APPEND);
}
Sign up to request clarification or add additional context in comments.

5 Comments

+1 that's better than my answer, I couldn't understand the main idea
thanks a lot, that worked exactly the way i want it to :) i think i will now need a way using preg_split to check if file has more than one name to perform a function
how can i add a code to check if more than 1 $name is in $ip_file to execute another command ? thanks in advance
@GuestofHonor if(count(file($ip_file)) > 1)
@FuzzyTree, that command didn't work, as it banned all my users :) command counted $ip_file and not the $name inside the file :)
1

Is this what you need?

<?php
$ip_file = "ips/".$ip.".txt";
$name = $name."\n";

if (file_exists($ip_file)) {

    $valueInFile = file_get_contents($ip_file, true);

    if ($valueInFile == $name) {
       //Do something
    }
} else {
    $logip = fopen($ip_file,"a", 1);
    fwrite($logip, $name);  
    fclose($logip);
}
return;
?>

From: http://php.net/manual/en/function.file-exists.php

3 Comments

This answer is not related to duplicate values.
i'm already writing that filename.txt so i know it exists, i need to verify the data inside it
oh sorry, then maybe the "a" parameter? Is not from append?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.