Jump to content

don't submit form if ip banned?


phpnoobie9

Recommended Posts

I have a table "banned" which has ips of banned ips.

I don't want the banned ip to submit the form...but it doesn't seem to work.

I just keep getting my else statement.

 

Not all the code but this is pretty much what it looks like

if (isset ($_POST['submit'])) {
$ip = $_SERVER['REMOTE_ADDR'];
$banquery = mysql_query("SELECT ip FROM banned");
   if($ip !== $banquery) {//Check for banned ip
   not banned do this....
   }
   else {
   if banned do this..
   }
}

Link to comment
https://forums.phpfreaks.com/topic/98535-dont-submit-form-if-ip-banned/
Share on other sites

You have no WHERE clause on the query and you have to extract the data from the result set (or in this case just do a record count)

 

<?php
if (isset ($_POST['submit'])) {
$ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$banquery = mysql_query("SELECT ip FROM banned WHERE ip = '$ip'");
   if(mysql_num_rows($banquery) !== 0) {//Check for banned ip
      not banned do this....
   }
   else
   {
      if banned do this..
   }
}
?>

Still doesn't seem to work. Escaping the remote_addr inserts a blank in the table.

 

There is no INSERT in that code - no way it could insert a blank line. Although, the logic in the IF staemetn needs to be reversed to work correctly

 

if(mysql_num_rows($banquery) == 0) {//Check for banned ip

oh that's because I had an insert for the ip..

I'm wondering why it works if it is negate....

if i have a banned ip with !== 0 it does the if statement which should be done if it == 0...

if i change it to just ==0 like you said it does the else statement which should be done if ip !==0

 

It works like I wanted to if I negate it, but looking at the code it should do the opposite..?

I don't think you are looking at it correctly. The IF statement was change to use mysql_num_rows(). So if the query finds 1 or more banned records, then mysql_num_rows() will be greater than 0. IF the ip is NOT in the banned records list then the result of mysql_num_rows() will be 0.

   if(mysql_num_rows($banquery) == 0) {//Check for banned ip
      //Number of records from banned IP list matching this IP was 0
      not banned do this....
   }
   else
   {
      //Number of records from banned IP list matching this IP was > 0
      //In other words, the ip exists int he banned records list
      if banned do this..
   }

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.