Forum Moderators: coopster

Message Too Old, No Replies

Function problem!

2 different problems with my username checker.

         

GamingLoft

4:41 pm on Feb 9, 2008 (gmt 0)

10+ Year Member



Ok, i have had my function working perfectly before, but then i noticed all my else statements were stopping the function from bringing up ALL the errors that the username had on the first run, so if someone was trying to register at my site it'd be like

in this example just pretend badword is a bad word o.0

[Leaves username blank]
-- You left the username field empty
[Enters username (@ba)]
-- Username must be atleast 4 characters long
[Adds on to username (@badword)]
-- username must contain only letters, numbers and the following characters: .?! - _
[Takes out the @ (badword)]
-- Usernames are not aloud to contain foul language.
[Rewrites username and its good]
SEE HOW LONG OF A PROCESS THAT IS, i think it'd be better just to give them all the errors that showed up rather than do the else statement, i also made a example for this..

again badword is a badword :P

[Leaves username blank]
-- You left the username field empty
[Enters username (@ba)]
-- Username must be atleast 4 characters long
-- username must contain only letters, numbers and the following characters: .?! - _
[Makes username longer and takes out @ (badword)
-- Usernames are not aloud to contain foul language.
[Fixes username and makes it valid]

** well obviously most people aren't going to be dumb enough to leave the username field empty, then make their username less than 4-6 characters long and use innapropriate language in their name. but that was just an example. anyways ill show you what happens when i use my username function!

[Leave username field blank]
--You left the username field empty!
--Username must be atleast 4 characters in length!
--Usernames are not aloud to contain foul language.
[Enter username [@ba]
--You're username must contain only letters, numbers and the following characters: .?! - _
--Username must be atleast 4 characters in length!
--Usernames are not aloud to contain foul language.
(@ba = not a badword, just part of one)
[Take out @ and make longer (badword)]
--Usernames are not aloud to contain foul language.
[Make username not contain foul language]
--Usernames are not aloud to contain foul language.

so now that you know how things USED to work and how i WANT them to work and how they ARE working. ill tell you my problems.

1)
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 the strlen > 1, 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!"

2)
"Usernames are not aloud to contain foul language" comes up no matter WHAT!

Here is my function

ps: $badwords is a variable at the top of my functions file.


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 4:46 pm (utc) on Feb. 9, 2008]

darrenG

2:49 pm on Feb 10, 2008 (gmt 0)

10+ Year Member



Could it be that the function should return true if the username is valid. At present it returns $error array if it is invalid, and false if error array is empty.

Also, bear in mind that when evaluating the result of the function to use:

$valid = checkUsername($u);

if($valid === true)
// username valid
else
// username not valid
// print each error message

Do not use:

if($valid == true)
// will be true if $valid is true OR $valid is not null ie is not an empty variable or empty array

Im not sure if that is what the problem is, but something to check out if you haven't already.

GamingLoft

6:50 pm on Feb 10, 2008 (gmt 0)

10+ Year Member



ok, so about the true/false thing, i was once told that it is much safer to use false there, i can't remember the reason why but maybe someone can back me up :P.

and i found out why when the field was left empty the 2 messages came up...
example
"
You lef the username field empty.
Username must be atleast 4 characters in length!
"

then later i corrected my problem with the username can not contain foul language...

but i have another problem...

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.

[edited by: GamingLoft at 7:35 pm (utc) on Feb. 10, 2008]

GamingLoft

5:20 pm on Feb 11, 2008 (gmt 0)

10+ Year Member



nvm this problem solved.