when registering.
how do i make it so that its like with php

if($username matches any of these words){exit("that username is not available to be used. sorry it is forbidden");}

like that kind of thing but see i was thiinking it would be better if all the words(cause there will be A LOT of them) should be in a text file.
im totally lost on how to do any of this so how can i do this please does anyone have any ideas.?

Recommended Answers

All 8 Replies

Wouldn't it be easier to have a table in the database to hold the restricted names, and then just query that table.

no i dont know how to do that either i just would like a nice file separated by spaces with all the words you cant use to register a username at my site. unless having it in mysql is easier and better for the server.

Find out how to get text files using php.

Using this text file of space seperated words generate an array using explode();

loop through the array and compare the username to the string. I think the function is instr();

If instr() returns true then an illegal word appears in the username.

I would put a more in depth function for you - but I am working right now and I am very busy. Sorry.

You can also try just adding a simple array in a php file, then checking it with inarray($array, $username) in an if.
Another option would be to do an sql query:

INSERT INTO `users` ... WHERE (SELECT id FROM `invalidusers` WHERE `username` = `{$username}` IS NULL)
Member Avatar for diafol

Just curious, if you don't know how to test a word against a list in a db table, how are you storing your user data?

Codejoust has the easiest solution if you don't want to get your hands dirty with mysql.

In a new php file (let's call it banned.php in the includes folder), have something like:

$banned = array("bad_word","really_bad_word","oh_my_dear",...etc...);

Then in your handling page for the registration form:

include('includes/banned.php');
if(inarray($banned,$username)){
//do this - naughty word!
}else{
//do that - ok so far
}

You can also try just adding a simple array in a php file, then checking it with inarray($array, $username) in an if.
Another option would be to do an sql query:

INSERT INTO `users` ... WHERE (SELECT id FROM `invalidusers` WHERE `username` = `{$username}` IS NULL)

You can't place a WHERE clause inside a SELECT statement.

http://dev.mysql.com/doc/refman/5.1/en/insert.html

However, you can do the SELECT and then INSERT if needed.

SELECT 1 FROM `invalidusers` WHERE `username` = `{$username}` LIMIT 1

Then insert if the username if no result was returned.

You could also make sure you don't have a duplicate username, or restricted one in a single query:

SELECT 1 FROM `invalidusers` WHERE `username` = `{$username}` LIMIT 1
UNION
SELECT 1 FROM `users` WHERE `username` = `{$username}` LIMIT 1

It would probably be better however, if you have a unique index on the username column.

That way you can just insert the usernames you don't want to be used into the users table before hand.

Any inserts of existing usernames during registering, will cause a duplicate entry, and thus return an errror. You can catch the error in PHP in give the user the message that their username is a not allowed.

For checking before hand, you just do:

select id from users where username = '$username' LIMIT 1

You can also have a column named "restricted" if you want to convert an existing registered username to a restricted one later on. Like someone registered "superman" and you don't like that username, just do:

update users set restricted = 1 where username = 'superman' LIMIT 1

That essentially would ban the user, and prevent anyone from registering that same username.

It also makes it easy to convert a restricted username to a valid one. Say you now want "superman" for yourself.

update users set password = sha1('mypass'), restricted = 0 where username = 'superman' limit 1

You can also try just adding a simple array in a php file, then checking it with inarray($array, $username) in an if.

this one. this is the one i was thinking about but couldnt come up with i think.

but i would need to be like
$array = "file contents";
before the inArray()
but i dont know how to put the file contents into th array and are arrays sepearated by something?

Just curious, if you don't know how to test a word against a list in a db table, how are you storing your user data?

Codejoust has the easiest solution if you don't want to get your hands dirty with mysql.

In a new php file (let's call it banned.php in the includes folder), have something like:

$banned = array("bad_word","really_bad_word","oh_my_dear",...etc...);

Then in your handling page for the registration form:

include('includes/banned.php');
if(inarray($banned,$username)){
//do this - naughty word!
}else{
//do that - ok so far
}

this is exactly what i meant. i know how to do mysql ok i just think its better using a file. i guess a php file is what ill use instead of text because its the only one that makes sense. thanks

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.