Jump to content

[SOLVED] Data from MySQL DB Table into an Array for IP Ban Script


Recommended Posts

Hey There,

 

Below is a part of an IP Banning script. The problem is that the original script had IP Arrays but I want it to grab IP's from a database table. I tried using while and it didn't work. It seems that I NEED to use foreach(); How would I do this?

 

<?php // Allows you to ban people from viewing your website.
$getip = $_SERVER["REMOTE_ADDR"];
$getdate = date( "l dS of F Y" ); 
$gettime = date( "h:i:sa (@B" ); 

$allbans = "SELECT * FROM bannedips";
$listbans = mysql_query($allbans);

foreach($listbans as $banned) { 
$ip = $_SERVER['REMOTE_ADDR'];
if($ip == $banned){ 
echo "It seems you have been banned from viewing this website.";
echo "<br />";
echo "If you think you have been banned in error please contact me.";
$fp = fopen("ip_data.dat",  "a");  
fputs($fp, "**BANNED** Visit logged on $getdate at $gettime internet time) for IP: $getip
");
fputs($fp, "");
fclose($fp);
exit(); 
} 
}

 

For your info, It currently shows the error:

 

Warning: Invalid argument supplied for foreach() in /www/110mb.com/r/u/n/e/s/c/a/p/runescapez/htdocs/require/baninclude.php on line 64

 

Thanks in advance,

Dan.

Try

 


<?php // Allows you to ban people from viewing your website.
$getip = $_SERVER["REMOTE_ADDR"];
$getdate = date( "l dS of F Y" ); 
$gettime = date( "h:i:sa (@B" ); 

$allbans = "SELECT * FROM bannedips";
$listofbans = mysql_query($allbans);
$listbans = mysql_fetch_array($listofbans);

foreach($listbans as $banned) { 
$ip = $_SERVER['REMOTE_ADDR'];
if($ip == $banned){ 
echo "It seems you have been banned from viewing this website.";
echo "<br />";
echo "If you think you have been banned in error please contact me.";
$fp = fopen("ip_data.dat",  "a");  
fputs($fp, "**BANNED** Visit logged on $getdate at $gettime internet time) for IP: $getip
");
fputs($fp, "");
fclose($fp);
exit(); 
} 
}

You actually need to get your mysql server to do the work for you and put the IP address you want to find in a WHERE clause in your query and then just execute the query and test how many rows, if any, were found.

 

Don't fetch all the rows and compare them in php. That would be a waste of time and a waste of a perfectly good database server.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.