Baving Posted September 17, 2006 Share Posted September 17, 2006 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 Link to comment https://forums.phpfreaks.com/topic/21095-registration-disallowed-names/ Share on other sites More sharing options...
Mr_Pancakes Posted September 17, 2006 Share Posted September 17, 2006 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.[code]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; }}[/code] Link to comment https://forums.phpfreaks.com/topic/21095-registration-disallowed-names/#findComment-93692 Share on other sites More sharing options...
Baving Posted September 18, 2006 Author Share Posted September 18, 2006 I have this function: -[code]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;}} [/code]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 Link to comment https://forums.phpfreaks.com/topic/21095-registration-disallowed-names/#findComment-94112 Share on other sites More sharing options...
Mr_Pancakes Posted September 19, 2006 Share Posted September 19, 2006 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:[code]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[/code] Link to comment https://forums.phpfreaks.com/topic/21095-registration-disallowed-names/#findComment-94413 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.