Jump to content

[resolved] username blacklist error


darkcarnival

Recommended Posts

im back again,

with 1 problem left to go im stuck on a solution for my username blacklist checker.

it can find an exact item like admin or webmaster but if wanted to look for:

siteadmin it will not block that.

i want to be able to block any occurance of admin.

I've troied several things but none seem to do it. her is the code im using:

[code]function blacklisted_usernames($value){

$q = mysql_query("SELECT * FROM ebb_blacklist") or die(mysql_error());
while ($row = mysql_fetch_assoc($q)){

if (strstr($row['blacklisted_username'], $value) !== false){
$blklist = 1;
}else{
$blklist = 0;
}
}
return ($blklist);
}[/code]

I've tried several other things but nothing has worked, any input is welcomed.
Link to comment
https://forums.phpfreaks.com/topic/18365-resolved-username-blacklist-error/
Share on other sites

hitman6003's suggestion would be much more efficient then using a loop for no reason.

[code=php:0]
function blacklisted_usernames($value) {
  $q = mysql_query("SELECT blacklisted_username FROM ebb_blacklist WHERE blacklisted_username LIKE '%$value%'") or die(mysql_error());
  return mysql_num_rows($q);
}
[/code]
here is the full code im using:

[code]#db connection stuff.
$username = "siteadmin";
function blacklisted_usernames($value){


$q = mysql_query("SELECT * FROM ebb_blacklist where blacklisted_username like '$value'") or die(mysql_error());
$n = mysql_num_rows($q);

if($n == 1){
$blklist = 1;
}else{
$blklist = 0;
}
return ($blklist);
}
#check it
$blklist = blacklisted_usernames($username);
if ($blklist == 1) {
echo "username ban!!";
}elseif($blklist == 0){
echo "Your good to go, username pass test.";
}else{
echo "blank.$blklist";
}
?>[/code]
Your code will fail if your query finds more than one record containing the word admin.

Does this work?

[code=php:0]
<?php

  function blacklisted_usernames($value) {
      $q = mysql_query("SELECT blacklisted_username FROM ebb_blacklist WHERE blacklisted_username LIKE '%$value%'") or die(mysql_error());
      return mysql_num_rows($q);
  }
  $name = 'siteadmin';
  if (blacklisted_usernames($name)) {
      echo "sorry, that $name is a banned username";
  }

?>
[/code]
Sorry... I dont know what I was thinking. You'd need to check the other way around. This might be the easiest solution.

[code=php:0]
function blacklisted_usernames($value) {
  $result = mysql_query("SELECT blacklisted_username FROM ebb_blacklist") or die(mysql_error());
  if ($result) {
    while($row = mysql_fetch_assoc($result)) {
      if (strstr($value,$row['blacklisted_username'])) {
        return true;
      }
    }
    return false;     
  }     
}
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.