Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7629555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:51:51+00:00 2026-05-31T05:51:51+00:00

I am writing a small script which checks if a users has his username

  • 0

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:

$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);
    }

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.

Thank you for your time and help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:51:52+00:00Added an answer on May 31, 2026 at 5:51 am

    EDIT TO ADD: The accepted answer is great. As a counterpoint, I’ve modified your code below in the event that you want to continue to read line by line instead of all it once – the file is unlikely to get so big that doing it in one chunk is ever a problem, but making that kind of assumption always makes me very slightly nervous.


    I see a couple of issues, here:

    Even when you find the username in the file, you carry on through the rest of the file, getting false negatives. Try:

    if (strpos($buffer, $stringData) == TRUE) {
        echo "This username is already whitelisted.";
        break;
    }
    

    Your else if will trigger whenever the script finds a line that doesn’t match the submitted username, rather than when it gets to the end of the file. You’ll need to move that check outside of the loop so that the new username is only added once. Altogether, now:

    $fh = @fopen("whitelist.txt", 'a+');
    $stringData = $_POST[usr]. "\n";
    if ($fh)
        {
            $found = false;
            while (!feof($fh))
            {
                $buffer = fgets($fh);
                if (strpos($buffer, $stringData) == TRUE) {
                    echo "This username is already whitelisted.";
                    $found = true;
                    break;
            }
            if (!$found) {
                fwrite($fh, $stringData);
                echo "Username ". $stringData. " is now whitelisted.";
            }
            fclose($fh);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a small C application that launchs a Matlab script (.m file). I
I'm writing a small Python script which requires resolving hosts IPs or domain names.
I was writing this small script the other day.It is a small program which
I am currently writing a small script that checks the contents of each string.
I am writing a small script to test if a form has been altered
Hi I am writing a small jQuery script which will toggle a hidden Top-Panel
i'm writing a small python utility which will be consumed by moderately non-technical users
I'm writing a small Python script which will periodically pull information from a 3rd
I'm a complete newb writing out a small little script in which the third
I'm writing a small shell script that needs to reverse the lines of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.