Forum Moderators: coopster
$badwords = "***?k¦[kc]***¦**[zs]e?hole?¦*[sz]?[zs]hole?¦face$¦pi[zs]?[zs]¦****¦****¦****?k¦**[zs]?[sz]er¦*****¦******r¦**[zs]?[zs][yi]¦**?oke?[iy]¦***?ch¦**?[sz]**?d¦*[kc]***¦bal?l[sz]¦***?lo[kc]?[ck][zs]¦*****s?¦****[zs]ter¦****[sz]t¦***[iu][sz]¦***[sz]¦**?k¦***¦***?k¦head$¦(you )¦( you )¦(u )¦( u )¦( of?f$)¦( of?f )¦[***]{3,}";
That is at the top of my functions file right under my include for the DB connect file... i ***'d out the bad words because last time i posted a bad word finder, my post had gotten in trouble.
anyways, my checker was working wonderfully untill i attempted to make my badwords all in one variable..
heres my new line 36-40ish...
$check= eregi($badwords, $username);
if ($check) {
$errors[] = "Usernames are not aloud to contain foul language.";
}
heres the old
$check= eregi("***?k¦[kc]***¦**[zs]e?hole?¦*[sz]?[zs]hole?¦face$¦pi[zs]?[zs]¦****¦****¦****?k¦**[zs]?[sz]er¦*****¦******r¦**[zs]?[zs][yi]¦**?oke?[iy]¦***?ch¦**?[sz]**?d¦*[kc]***¦bal?l[sz]¦***?lo[kc]?[ck][zs]¦*****s?¦****[zs]ter¦****[sz]t¦***[iu][sz]¦***[sz]¦**?k¦***¦***?k¦head$¦(you )¦( you )¦(u )¦( u )¦( of?f$)¦( of?f )¦[***]{3,}", $username);
if ($check) {
$errors[] = "Usernames are not aloud to contain foul language.";
}
and here is my entire function...
function checkUsername($username)
{
$errors = array();
$username = stripslashes($username);
if (strlen($username) < 1){ // if the username is 0 characters long then it tells you its empty!
$errors[] = "You left the username field empty!";
}
else{
if (eregi('^[[:alnum:]\?\!\-\_\'\-]', stripslashes(trim($username)) )) {//must be 4-12 letters/chars long .?! - _ ' all aloud + nums and letters
$user = mysql_real_escape_string($username);
$query_usernamecheck = "SELECT username FROM hfs_users WHERE username = '$username'";
$result = @mysql_query($query_usernamecheck);
$num_samename = @mysql_num_rows($result_usernamecheck);
if ($num_samename> 0) { //checks if anyone is using this name $errors[] = "This name is not availible.";//error message if someones using name
}
else {
if (strlen($username) < 4){
$errors[] = "Username must be atleast 4 characters in length!";
}
else
{
if (strlen($username) > 12){
$errors[] = "Username can not be more than 12 characters in length!";
}
else
{
$check= eregi($badwords, $username);
if ($check) {
$errors[] = "Usernames are not aloud to contain foul language.";
}
else
{
$user = mysql_real_escape_string($username);
$query_blockedname = "SELECT username FROM hfs_blockedname WHERE username = '$username'";
$result_blockedname = @mysql_query($query_blockedname);
$num_blockedname = @mysql_num_rows($result_usernamecheck);
if ($num_blockedname> 0) {
$errors[] = "The username you entered is not aloud.";//error message if name is put in unacceptable list
}}}}}}
else {
$errors[] = "You're username must contain only letters, numbers and the following characters: .?! - _";
}}
if(!empty($errors)){ return $errors; }
return false;
}
//End Check Username//
//Check for Badwords//
function bad_word($intext) {
$check= eregi("fuc?k¦[kc]unt¦ar[zs]e?hole?¦a[sz]?[zs]hole?¦face$¦pi[zs]?[zs]¦#*$!¦wank¦pric?k¦to[zs]?[sz]er¦#*$!¦vaginer¦pu[zs]?[zs][yi]¦breath¦mo?oke?[iy]¦stink¦bit?ch¦bar?[sz]tar?d¦s[kc]rew¦bal?l[sz]¦bol?lo[kc]?[ck][zs]¦gonads?¦idiot¦mole[zs]ter¦mole[sz]t¦pen[iu][sz]¦anu[sz]¦coc?k¦horrible¦dirty?¦filthy?¦dic?k¦head$¦slime¦kille[rd]¦puke¦vomit¦suck¦spastic¦spaz¦bugg?ery?¦(you )¦( you )¦(u )¦( u )¦( of?f$)¦( of?f )¦[asz]{3,}", $intext);
if ($check) {
$errors[] = "Usernames are not aloud to contain foul language.";
}
if(!empty($errors)){ return $errors; }
return true;
}
and here is the error im getting
Warning: eregi() [function.eregi]: REG_EMPTY in /home/EXMPL/public_html/site/functions.php on line 36 HELP PLEASE
$badwords = 'green¦vegetables¦exercise'; // ;)
function checkUsername($username) {
..
}
$badwords = 'green¦vegetables¦exercise';
function checkUsername($username) {
global $badwords;
..
}
You also left in the real version of the words in your code...although I dont think that many people are going to be surprised at the language (this is supposed to be for webmasters, so I'm going to assume people are older than about 12 and therefor already know all of those words). In fact it may well be more educational to leave it in so people can see what you are blocking. As if someone is searching for a badword blocking script its not going to help blocking a load of *'s after all ;)
edit//
eek if you look up at my first post you will notice i accidentally copied the next function from my file "//Check for Badwords"
and the editing time is up, anyways anyone who wants to see my badwords function just look up -.-'
also, thanks a lot for telling me about global, i had never done anything like this before, i have a feeling its going to work, working on it now.
thanks in advanced!
/// THIS IS A NEW POST, OK!
i tried the global like this...
function checkUsername($username)
{
$errors = array();
global $badwords;
sorry about the badwords, but it is for educational purposes :)
badwords
$badwords = "\"fuc?k¦[kc]unt¦ar[zs]e?hole?¦a[sz]?[zs]hole?¦face$¦pi[zs]?[zs]¦#*$!¦wank¦pric?k¦to[zs]?[sz]er¦#*$!¦vaginer¦pu[zs]?[zs][yi]¦mo?oke?[iy]¦bit?ch¦bar?[sz]tar?d¦s[kc]rew¦bal?l[sz]¦bol?lo[kc]?[ck][zs]¦gonads?¦mole[zs]ter¦mole[sz]t¦pen[iu][sz]¦anu[sz]¦coc?k¦cum¦dic?k¦head$¦(you )¦( you )¦(u )¦( u )¦( of?f$)¦( of?f )¦[asz]{3,}\"";
and here is my complete function..
function checkUsername($username)
{
$errors = array();
global $badwords;
$username = stripslashes($username);
if (strlen($username) < 1){ // if the username is 0 characters long then it tells you its empty!
$errors[] = "You left the username field empty!";
}
else{
if (eregi('^[[:alnum:]\?\!\-\_\'\-]', stripslashes(trim($username)) )) {//must be 4-12 letters/chars long .?! - _ ' all aloud + nums and letters
$user = mysql_real_escape_string($username);
$query_usernamecheck = "SELECT username FROM hfs_users WHERE username = '$username'";
$result = @mysql_query($query_usernamecheck);
$num_samename = @mysql_num_rows($result_usernamecheck);
if ($num_samename> 0) { //checks if anyone is using this name
$errors[] = "This name is not availible.";//error message if someones using name
}
else {
if (strlen($username) < 4){
$errors[] = "Username must be atleast 4 characters in length!";
}
else
{
if (strlen($username) > 12){
$errors[] = "Username can not be more than 12 characters in length!";
}
else
{
$check= eregi($badwords, $username);
if ($check) {
$errors[] = "Usernames are not aloud to contain foul language.";
}
else
{
$user = mysql_real_escape_string($username);
$query_blockedname = "SELECT username FROM hfs_blockedname WHERE username = '$username'";
$result_blockedname = @mysql_query($query_blockedname);
$num_blockedname = @mysql_num_rows($result_usernamecheck);
if ($num_blockedname> 0) {
$errors[] = "The username you entered is not aloud.";//error message if name is put in unacceptable list
}}}}}}
else {
$errors[] = "You're username must contain only letters, numbers and the following characters: .?! - _";
}}
if(!empty($errors)){ return $errors; }
return false;
}
PLEASE HELP!
[edited by: GamingLoft at 2:59 am (utc) on Feb. 8, 2008]
Although this may not help with the actual problem that you are facing you could make you code easier to read by using an array.
$badwords = array('green', 'vegetables');
foreach ($badwords as $word) {
$check = eregi($word, $username);
if ($check) {
$error[] = "Usernames are not aloud to contain foul language.";
}
}
ok, i took out most of my else statements except for one, because i want my users to know all of the errors that the entered username had, not just the first error that is found...
<u>PROBLEM 1:</u>
so i made it so if it was empty than no other errors would come up (so the one that checks if its under 4 characters wont come up..)
alright so i added a else statement after that, but it did nothing! when the name is empty it still says "Username must be atleast 4 characters in length!" after the "You left the username field empty!"
<u>PROBLEM 2:</u>
"Usernames are not aloud to contain foul language." comes up no matter what and i cant figure out why!
here is my function... also i have a array at the top of my file for $badwords...
function checkUsername($username)
{
$errors = array();
global $badwords;
$username = stripslashes($username);
if (strlen($username) < 1){ // if the username is 0 characters long then it tells you its empty!
$errors[] = "You left the username field empty!";
}
else{
if (!eregi('^[[:alnum:]\?\!\-\_\'\-]', stripslashes(trim($username)) )) {//must be 4-12 letters/chars long .?! - _ ' all aloud + nums and letters
$errors[] = "You're username must contain only letters, numbers and the following characters: .?! - _";
}
$user = mysql_real_escape_string($username);
$query_usernamecheck = "SELECT username FROM hfs_users WHERE username = '$username'";
$result = @mysql_query($query_usernamecheck);
$num_samename = @mysql_num_rows($result_usernamecheck);
if ($num_samename> 0) { //checks if anyone is using this name
$errors[] = "This name is not availible.";//error message if someones using name
}
}
if (strlen($username) < 4){
$errors[] = "Username must be atleast 4 characters in length!";
}
if (strlen($username) > 12){
$errors[] = "Username can not be more than 12 characters in length!";
}
foreach ($badwords as $bword) {
if (eregi($bword, $username)) {
}}
$errors[] = "Usernames are not aloud to contain foul language.";
$user = mysql_real_escape_string($username);
$query_blockedname = "SELECT username FROM hfs_blockedname WHERE username = '$username'";
$result_blockedname = @mysql_query($query_blockedname);
$num_blockedname = @mysql_num_rows($result_usernamecheck);
if ($num_blockedname> 0) {
$errors[] = "The username you entered is not aloud.";//error message if name is put in unacceptable list
}
if(!empty($errors)){ return $errors; }
return false;
}
[edited by: GamingLoft at 10:42 pm (utc) on Feb. 8, 2008]
function checkBadLanguage($username) {
global $badwords;
foreach ($badwords as $bword) {
if (eregi($bword, $username) {
return true; // true as there is a bad word
}
else {
return false; // false as there is no bad word
}
}
}
function checkAlreadyRegistered($username) {
$username = mysql_real_escape_string($username);
$query_usernamecheck = "SELECT username FROM hfs_users WHERE username = '$username'";
$result = @mysql_query($query_usernamecheck);
$num_samename = @mysql_num_rows($result_usernamecheck);
if ($num_samename!= 0) {
//!= 0 will add this error if there is a problem, or that name is already taken
return true;
}
else {
return false;
}
}
function checkBlockedList($username) {
$username = mysql_real_escape_string($username);
$query_blockedname = "SELECT username FROM hfs_blockedname WHERE username = '$username'";
$result_blockedname = @mysql_query($query_blockedname);
$num_blockedname = @mysql_num_rows($result_usernamecheck);
if ($num_blockedname!= 0) {
//!= 0 for same reason as the above elseif block
return true;
}
else {
return false;
}
}
function checkUsername($username) {
$errors = ''; //array();
$username = trim($username); // remove spaces from $username
if (get_magic_quotes_gpc()) { // more portable
$username = stripslashes($username);
}
// START CHECKS
// check length of $username
if (empty($username)) { // donesnt allow 0 as a user name
$errors = "You left the username field empty!";
return $errors;
}
elseif (strlen($username) < 4){
$errors = "Username must be atleast 4 characters in length!";
return $errors;
}
elseif (strlen($username) > 12){
$errors = "Username can not be more than 12 characters in length!";
return $errors;
}
// check name contains only correct characters
elseif (!eregi('^[[:alnum:]\?\!\-\_\'\-]',$username)) {
$errors = "You're username must contain only letters, numbers and the following characters: .?! - _";
return $errors;
}
// check for bad language
elseif (checkBadLanguage($username)){
$error = 'Usernames are not allowed to contain bad language';
return $errors;
}
// checks if anyone is using this name
elseif (checkAlreadyRegistered($username)){
$errors = "This name is not available.";
return $errors;
}
// checks if username on blocked list
elseif (checkBlockedList($username)){
$errors = "The username you entered is not aloud.";
return $errors;
}
// FINISHED CHECKS
else {
// to get through to here none of the other blocks should have been used.
return false;
} // end if, elseif, else checking blocks.
You may want to adjust that so all of the checks are run through first then they get a list of there errors.
Hopefully that will work for you.
everything is working fine but im wondering how do i setup my eregi to only find the first badword in the name.
cuz if i entered a name like "badword1-Badword2" the error
"Usernames are not aloud to contain foul language.
Usernames are not aloud to contain foul language."
would come up, now i only want this to come up once, so i believe that if i make my eregi find only the first coincidence of a badword in the name this will correct the problem, but i forget how to do that and i've search all afternoon :/
heres my eregi...
global $badwords;
foreach ($badwords as $bword) {
if (eregi($bword, $username)){
$errors[] = "Usernames are not aloud to contain foul language.";}}
i think i might have to use something else other than ereg, please help.
Using return will stop the script at that point and allow you to return a message. The reason for all those return statements in my code above.
Another option would be to call array_unique [uk.php.net] on the $error array to remove duplicates.
So if they trip 3 or 4 of your bad words then this will result in 3 or 4 of the same error messages that you can then remove.
So your error array will have all of the error messages your code has generated, however there will be no duplicates.