Malevolence Posted January 3, 2008 Share Posted January 3, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84323-solved-data-from-mysql-db-table-into-an-array-for-ip-ban-script/ Share on other sites More sharing options...
suttercain Posted January 3, 2008 Share Posted January 3, 2008 The problem is $listbans is not an array. Try mysql_fetch_array Quote Link to comment https://forums.phpfreaks.com/topic/84323-solved-data-from-mysql-db-table-into-an-array-for-ip-ban-script/#findComment-429423 Share on other sites More sharing options...
trq Posted January 3, 2008 Share Posted January 3, 2008 Your missing any call to mysql_fetch_assoc(). Quote Link to comment https://forums.phpfreaks.com/topic/84323-solved-data-from-mysql-db-table-into-an-array-for-ip-ban-script/#findComment-429424 Share on other sites More sharing options...
suttercain Posted January 3, 2008 Share Posted January 3, 2008 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(); } } Quote Link to comment https://forums.phpfreaks.com/topic/84323-solved-data-from-mysql-db-table-into-an-array-for-ip-ban-script/#findComment-429426 Share on other sites More sharing options...
PFMaBiSmAd Posted January 3, 2008 Share Posted January 3, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84323-solved-data-from-mysql-db-table-into-an-array-for-ip-ban-script/#findComment-429438 Share on other sites More sharing options...
Malevolence Posted January 3, 2008 Author Share Posted January 3, 2008 Yep that worked. Wonder why when I tried it, it didn't work. OH RIGHT, you queried it, THEN you fetched the Arrays. Thanks, Dan. Quote Link to comment https://forums.phpfreaks.com/topic/84323-solved-data-from-mysql-db-table-into-an-array-for-ip-ban-script/#findComment-429446 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.