Jump to content


Photo

Registration Disallowed Names


  • Please log in to reply
3 replies to this topic

#1 Baving

Baving
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 17 September 2006 - 09:10 PM

Hello,

On my registration script it checks for some disallowed usernames in a database which are not allowed to be registered, for example moderator.

What code could I use in php / mysql query to check if someone is trying to register disallowed names?

Also it needs to be able to use a sort of wildcard. So if I tried to register moderator-05 it wouldn't allow it. With moderator being the banned word.

Thanks

#2 Mr_Pancakes

Mr_Pancakes
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationSouthwest USA

Posted 17 September 2006 - 09:36 PM

use where $needle is your inputted usernames and $haystack is a string of your disallowed usernames. you could either hardcode these prohibitted usernames in $haystack, or better yet, store them in your db and query for them and store them in $haystack before you call the function.

function is_substr($needle, $haystack){
       $pos = strpos($haystack, $needle);
 
       if ($pos === false) {
               // inputted username was not found as a disallowed value.
               return false;
       } else {
               // inputted username is not allowed.
               return true;
       }
}


#3 Baving

Baving
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 18 September 2006 - 06:11 PM

I have this function: -

function name_filter($name) {

  $name = mysql_escape_string(strip_tags($name));
  $query = mysql_query("SELECT `word` FROM filter WHERE type = 'N'");

	$word = mysql_fetch_array($query);
	
	foreach ($word as $check) {
  
 
   if(preg_match("/$check/", $name)) return TRUE;

}}

But using preg_match is there any way to include like a * character to use as a wild card.

So say *php would block Greatphp but not Greathp

#4 Mr_Pancakes

Mr_Pancakes
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationSouthwest USA

Posted 19 September 2006 - 03:46 AM

do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster. i think i might have what you're looking for.

try:
function name_filter($name) {

   $name = mysql_escape_string(strip_tags($name));
   $query = mysql_query("SELECT `word` FROM filter WHERE type = 'N'");
   $word = mysql_fetch_array($query);

   foreach ($word as $check) {
      // swap out the preg_match() function with:
      $pos = strpos($check, $name);
 
       if ($pos === false) {
               // the $check value was not found in $name, continue.
       } else {
               // the $check value was found in $name, disallow username.
       }

   } // end foreach
} // end function





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users