Problems with reading and searching thru txt file with PHP
<p>I am writing a small script which checks if a users has his username in file called whitelist.txt or not. If the username is not found its added. This is the script:</p>

<pre><code>$fh = @fopen("whitelist.txt", 'a+');
$stringData = $_POST[usr]. "\n";
if ($fh)
    {
        while (!feof($fh))
        {
            $buffer = fgets($fh);
            if (strpos($buffer, $stringData) == TRUE)
                echo "This username is already whitelisted.";
            elseif (strpos($buffer, $stringData) == FALSE) {
                    fwrite($fh, $stringData);
                    echo "Username ". $stringData. " is now whitelisted.";
                    }
        }
        fclose($fh);
    }
</code></pre>

<p>What I get now if I enter a new username first time, all is ok. The second time I enter a new username it gets dubbled. Problems go on: if I enter an existing username it gets added twice and the message "This username is already whitelisted." is not shown. Every username is in a new row.</p>

<p>Thank you for your time and help!</p>
