mysql - PHP best practice to handle IP block and flood control for user login -


my every coding & algorithm set ok, required advice, if i'm going direction or not. have 2 concerns advice/review required.

concern i: algorithm

in login page, don't use google recaptcha. have following logic control flood & block ip:

  • for each non user failed login attempt, store ip on mysql "flood" table ip & time.
  • this way store & check maximum 20 attempts in last 2 hours. if occurs, delete flood records related ip & add ip "block_ip" mysql table.

for each of our php pages(30+) call following ip block function redirected blocked ip visitors "block.php" page.

function block_ip(){ $mysqli_2 = new mysqli(host, user, password, database);     if ($mysqli_2->connect_error) {     header("location: ../error.php?err=unable connect mysql");     exit();     }    $stmt_block_ip= $mysqli_2->prepare("select b_ip block_ip");       $stmt_block_ip->execute();    $stmt_block_ip->store_result();    $stmt_block_ip->bind_result($block_iip);    while( $stmt_block_ip->fetch()){   if(strpos($_server['remote_addr'],$block_iip) === 0)     {        header("location: ../block.php");         exit();     }     }   $stmt_block_ip->close();  } 
  • next admin plan is, if total number of block ip goes > 50, clear table copying blocked ip & asking server service provider add .htaccess redirect them.

is idea good? or should keep on blocked ip table, might slow page loading, isn't it? long list of blocked ip's? better idea?

concern ii: algorithm

on forgot-username.php & forgot-password.php page have google recaptcha. didn't add flood control algo here, non-user attempted redirect them register page.

for security concern approach okay?

my 1st concern got solved seems willparky suggestion: code below ip block checking should done faster. guess don't need use .htaccess & transfer blocked ip here , there.

  function block_ip(){   $viewer_ip = $_server['remote_addr'];    $mysqli_2 = new mysqli(host, user, password, database);     if ($mysqli_2->connect_error) {     header("location: ../error.php?err=unable connect mysql");     exit();     }   $stmt_block_ip= $mysqli_2->prepare("select b_ip block_ip b_ip =     ?");    $stmt_block_ip->bind_param('s',$viewer_ip);   $stmt_block_ip->execute();   $stmt_block_ip->store_result();   if($stmt_block_ip->num_rows == 1){     {      header("location: ../block.php");      exit();    }   }  $stmt_block_ip->close();   } 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -