PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Drachlen
Forum Contributor
Posts: 153 Joined: Fri Apr 25, 2003 1:16 am
Post
by Drachlen » Tue Jul 01, 2003 3:49 am
While i was testing my 'create account' page, i noticed it was allowing me to type in periods and spaces as the username. The code im using is:
Code: Select all
<?php
function is_username_valid($username) {
if(eregi("^[a-z0-9]",$username)) return TRUE;
else return FALSE;
}
?>
And i dont see any periods or spaces in it, how do i identify them?... Also, i was wondering how to use some function to find specific letters next to eachother in a variable, ie profanity. Id like to make sure people cant sign up with an offensive or racist name.
qartis
Forum Contributor
Posts: 271 Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:
Post
by qartis » Tue Jul 01, 2003 4:03 am
Personally, I hate regular expressions. I'd just go with
Code: Select all
if (!stristr($username," ") && !stristr($username,".") && !stristr($username,"fsck") && !stristr($username,"asp") && !stristr($username,"a$$")){
return true;
} else {
return false;
}
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Tue Jul 01, 2003 4:03 am
It's probably because your regular expression is just matching an alphnumeric character at the beginning of the username and then returning true. Try:
Code: Select all
function is_username_valid($username)
{
return (preg_match('/[^a-z0-9]/i', $username)) ? TRUE : FALSE;
}
Mac
qartis
Forum Contributor
Posts: 271 Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:
Post
by qartis » Tue Jul 01, 2003 4:07 am
He said he wants them together, so..
Code: Select all
if (eregi("^[a-z0-9]",$username) && !stristr($username," ") && !stristr($username,".") && !stristr($username,"fsck") && !stristr($username,"asp") && !stristr($username,"a$$")){
return true;
}
is how you'd do it.
EDIT: Although if you just want to make sure all the characters are letters, you might as well just do
Code: Select all
$username = trim($username);
if (!is_string($username)){
return false;
}
$badword = array("asp","bla","foo","bar");
foreach ($badword as $cuss){
if (stristr($username,$cuss)){
return false;
}
}
return true;
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Tue Jul 01, 2003 5:17 am
is_string() won't check for the non-alphanumeric characters but you can still do this with no regular expressions using
ctype_alnum() .
Mac