I have a registration form where I need to prevent people entering bad usernames. For example, people when they enter their username as virus, then the form should not be accepted. How can I prevent bad usernames in my Registration forms. Is there any function to do the same? Please help. Thanks in advance.

here you can use strpos.

if ( strpos($name, 'virus') !== false )
{
    echo 'virus not allowed try again';
}

I think an issue is if you were to use the name database and you filtered out the word data then it would still return false.

also a function
http://www.smseleem.com/php-function-check-bad-words-texts.html

If i have to include a file as badwords.txt, then what code to write?

you're going to spend a lot of time reinventing the wheel here. perhaps use an open source registration script. it will save you hours of work, and thousands of headaches.

Member Avatar for diafol

Have a database table (or array) with all the banned words - then use preg_match($pattern,$username) or similar, where $pattern could be,e.g.

"/(virus|git|fool)/i"

If you have an array of banned words:

$banned = array('virus','god','blast'...);
$pattern = "/(" . implode("|",$banned) . ")/i";

think it'll work - not tested.

you're going to spend a lot of time reinventing the wheel here. perhaps use an open source registration script. it will save you hours of work, and thousands of headaches.

Yes, I agree, I googled on this, there are no ways to stop the spammers to register swear words. Anyway, thanks for all your support.

Member Avatar for diafol

there are no ways to stop the spammers to register swear words

I thought I just gave you an example?

If you check out phpBB3 - they have a banned words table and these are checked against strings. You need to ensure that you check lowercase strings with lowercase banned words. You can do this with strtolower or mb_strtolower. The second is safer when using extended characters (accented / foreign characters).

Yep it's almost impossible :( ie -> v*rus or vir_us will not be detected on the solutions you have here (although they are correct)

I really all depends on what kind of spam we are talking about.. if it is a boot
I strongly recommend wcaptha
http://wcaptcha.wozia.pt/sample.php

If they are real human spammers, they will always find a way to do it :(

best regards

Member Avatar for diafol

Using these alternatives to me seem OK as the word isn't clear. But if you want to be as clean as possible...

f*ck or f_ck or whatever is an obvious one - you can still filter this out with something like "/((f[^aeiowy])ck(er|ing)*)/i".

The difficulty comes when users try to create - I'll use the work funk for an eexample:

f-u-n-k, f_u-n_k etc. You can only do so much as mentioned earlier, but general matches should wipe out 90%(?) of the dross. Keep an eye on users and change their names or ban them when you come across one that goes against the site rules.

ardav :)
I agree with you.. but I think that that filter will also stop words like funcky :D

I think the best solution to this problem.. is your solution, but it's not bullet prove :)
That is why I recommend looking at it from a different angle ;)

Member Avatar for diafol

Yeah, sure - the 'funk' example was just so that I didn't irritate or upset anybody.

Hi Ardav,

Can you help me with the exact code, let me try once more with your help...

Member Avatar for diafol

Try it yourself. Have a fiddle and come back with some code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.