NVG
NVG

Reputation: 3313

banning user ip through php or apache

Our application uses MySQL to store every banned IP. The IPs are banned automatically when certain events are triggered and they are added to the table bans for a given duration. The table is MyISAM, but very rarely an IP gets banned.

On each request that a player makes, we verify if his IP is among the banned IP's list and if his suspension is active. So sometimes this involves an useless DB request which influences the application's response time.

We were wondering if it wouldn't be a better choice to use APACHE and also write to file each banned IP, rather than performing an SQL query to verify if the IP of each visitor is among the banner IPs list.

How larger apps handle hundreds or thousands of banned IP's with many users active? Do they use databases or file writing?

Upvotes: 0

Views: 87

Answers (2)

rjdown
rjdown

Reputation: 9227

A database with a suitable index will be much faster than a simple file-based solution. If your response time is being affected by this query, something is very wrong with your code and/or database structure. If you need help with this, post your current code in a new question either here or preferably on codereview.

With that said, depending on what you want to do with these banned users, you could look into iptables which will block them from accessing your web server at all, thus removing the need for the query. There are a few libraries floating around that provide a PHP interface for this. Google "PHP iptables" for more info.

Upvotes: 3

Rod
Rod

Reputation: 21

If think you will end up banning thousands of IP. Then you must use database for speed. But if think at any time not more than 10-20 IP are banned you can choose to write in file. Banning from Apache is fast only when there are not too many IPs. Usually custom written function to verify IP are not efficient enough to be faster than database in searching through large number of IPs. I am talking in context of overall system resource use. But if you have no worries about eating up resources other than database then only writing in file can be justified.

Upvotes: 1

Related Questions