Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Always Invalid username with/without bad words

Tags:

php

passwords

I am tryiny to write code that denies any username that contains bad words. No matter what I do - I get "Invalid username."

$f = @fopen("censor.txt", "r");

$bw = fread($f, filesize("censor.txt"));
$banned_words = explode("\n", $bw);

function teststringforbadwords($wantusername, $banned_words)
{
    foreach ($banned_words as $banned_word) {
        if (stristr($wantusername, $banned_word)) {
            return FALSE;
        }
    }
    return TRUE;
}

if (!teststringforbadwords($wantusername, $banned_words)) {
    echo 'string is clean';
} else {
    echo('string contains banned words');
    $message = "Invalid username.";
}

@fclose($f);

I am currently learning php and have tried everything I can think of to get it to work - help!

like image 991
dewey Avatar asked Feb 08 '23 17:02

dewey


1 Answers

The function works fine, but you call it in not correct way, because the function return False if a bad word if matched:

if( teststringforbadwords( $wantusername, $banned_words ) )
{
    echo 'string is clean';
}
else
{
    echo('string contains banned words');
    $message = "Invalid username.";
}

Otherwise, if you want maintain coherence with function name, you have to invert True and False returns inside function.

like image 54
fusion3k Avatar answered Feb 10 '23 10:02

fusion3k