php/mysql - checking value in database.

Associate
Joined
11 Oct 2008
Posts
268
I have the following code which displays all the i.p addresses which are stored in my banned database.

I'm am trying to make it so if the users i.p address matches one in the database it echoes a banned message rather than the page.

I have tried a few different methods over the last few days but just cannot get it working. I'm not sure if im looking too deep into it, is there a simple way to do this perhaps?

Any help would be greatly appreciated.

PHP:
$sql = 'SELECT ip FROM banned ORDER by id DESC';
mysql_select_db('oakfarm');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{

echo <<<DB
     
         $row[ip] <br> 

DB;

}
 
It would be easier to put the user's ip address into a 'WHERE' clause rather than pulling back all addresses and checking in your code.
 
It would be easier to put the user's ip address into a 'WHERE' clause rather than pulling back all addresses and checking in your code.

This is better. Just grab the users IP, put it in a variable, use it in a WHERE clause, then do an IF statement; if it exists do this, else do that.
 
PHP:
$banned_ip = array();
$sql = "SELECT * FROM banned_ip WHERE active_block = 1";	  
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
	$banned_ip[$row['ip']] = $row;
}

if(isset($banned_ip[$_SERVER['REMOTE_ADDR']])){
	exit("Your IP (".$_SERVER['REMOTE_ADDR'].") is banned");
}

We use this for banned IP's on our sites.
 
Back
Top Bottom