conker87 Posted May 1, 2008 Share Posted May 1, 2008 I'm looking to use PHP to ban IP addresses rather than use .htaccess. I'm wondering what the best way to go about this is? Should I store these IP address in a file or create a little admin UI for myself to add addresses into a database. That said, how would I compare the $_SERVER['HTTP_HOST'] to the fetched array? Thanks guys. Link to comment https://forums.phpfreaks.com/topic/103721-ban-by-ip/ Share on other sites More sharing options...
Daney11 Posted May 1, 2008 Share Posted May 1, 2008 Id go with something like $ip = $_SERVER['HTTP_HOST']; if ($ip == 'BANNED') { echo "banned"; } else { } store the ip in a database with a field called isbanned or something. Link to comment https://forums.phpfreaks.com/topic/103721-ban-by-ip/#findComment-531047 Share on other sites More sharing options...
jonsjava Posted May 1, 2008 Share Posted May 1, 2008 easy way (not the best, but the easiest): <?php $banned_ip = array("10.10.10.10", "10.10.10.11"); if(in_array($_SERVER['REMOTE_ADDR'], $banned_ip)){ /* Get rid of 'em! */ } ?> Link to comment https://forums.phpfreaks.com/topic/103721-ban-by-ip/#findComment-531048 Share on other sites More sharing options...
conker87 Posted May 1, 2008 Author Share Posted May 1, 2008 Ahha. So basically: <?php //blah $ipArray = mysql_fetch_array($result) if (in_array($_SERVER['REMOTE_ADDR'], $ipArray)) { echo "banned"; } else { // entire code } ?> And thanks jonsjava, I knew it was remote_addr, mind went totally blank then. Link to comment https://forums.phpfreaks.com/topic/103721-ban-by-ip/#findComment-531053 Share on other sites More sharing options...
revraz Posted May 1, 2008 Share Posted May 1, 2008 Not sure why you think this will be effective, unless you work for a company and you know the user's IP won't change. Link to comment https://forums.phpfreaks.com/topic/103721-ban-by-ip/#findComment-531056 Share on other sites More sharing options...
jonsjava Posted May 1, 2008 Share Posted May 1, 2008 I prefer to block a C-class. Much more effective. Link to comment https://forums.phpfreaks.com/topic/103721-ban-by-ip/#findComment-531058 Share on other sites More sharing options...
PFMaBiSmAd Posted May 1, 2008 Share Posted May 1, 2008 If you put the ip addresses in a database (which you should anyway because you will end up with hundreds or thousands of banned addresses depending on how bad someone wants to visit your site or hot link your content), one per row, you can simply search using a query - SELECT * FROM your_table WHERE ip_column = '$_SERVER['REMOTE_ADDR']' Short answer, let the database do the work. (The fetch_array code you just posted would only work if you add a column for each IP and that would be a terrible table design.) Link to comment https://forums.phpfreaks.com/topic/103721-ban-by-ip/#findComment-531059 Share on other sites More sharing options...
conker87 Posted May 1, 2008 Author Share Posted May 1, 2008 A C-class eh? How would one go about this? Link to comment https://forums.phpfreaks.com/topic/103721-ban-by-ip/#findComment-531088 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.