Interval Grid
Interval Grid

Reputation: 187

Block IP Address, Query IP from database

I have an PHP code that the function is to block IP Address.

include ("includes/_db_.php");
$query_ip = mysql_query("SELECT * FROM t_ip_address");
while ($data_ip = mysql_fetch_array($query_ip))
{

$valid_ips = $data_ip['ip_address'];

if (!in_array($_SERVER['REMOTE_ADDR'],$valid_ips))
{
    echo '<div class="denied"><img src="images/stop.png"/><span class="titles">Access Denied</span><br><span class="content">Sorry you do not have authorized to access this page.</span></div>
        <div class="footer"><a href="../">Back to previous page</a></div>
        ';
        exit();
    }
    }

But facing problem now, the error is : Warning: in_array() expects parameter 2 to be array, string given in...

Anyone have an suggestions ?

Upvotes: 1

Views: 538

Answers (1)

Orangepill
Orangepill

Reputation: 24655

You are better off searching the database for $_SERVER["REMOTE_ADDR"] and checking to see if it returns anything.

$query_ip = mysql_query("SELECT * FROM t_ip_address where ip_address = '".mysql_real_escape_string($_SERVER["REMOTE_ADDR"])."'";
if (!mysql_fetch_array($query_ip)){
    echo '<div class="denied"><img src="images/stop.png"/><span class="titles">Access Denied</span><br><span class="content">Sorry you do not have authorized to access this page.          </span></div>
    <div class="footer"><a href="../">Back to previous page</a></div>
    ';
    exit();         
}  

Upvotes: 2

Related Questions