Block ip addresses automatically using PHP and htaccess

余生颓废 提交于 2019-12-11 09:38:48

问题


I have a login system that I am trying to make as secure as possible. After ten failed attempts, I would like to block the IP address. How can I open a file in PHP and add a line to my .htaccess file to block the IP Address? Here is my .htaccess file:

# Turn on RewriteEngine
RewriteEngine on

# Block IP addresses with too many failed attempts
RewriteCond %{REMOTE_ADDR} ^217.172.179.*$ [OR]
RewriteCond %{REMOTE_ADDR} ^217.172.180.*$
RewriteCond %{REQUEST_URI} !/error/blocked.php$ [NC]
RewriteRule ^(.*)$ /error/blocked.php [R,NC,L]

I would like the PHP file to add the line:

RewriteCond %{REMOTE_ADDR} ^IPHERE$ [OR]

in the appropriate spot.


回答1:


I wouldn't recommend editing the .htaccess file directly.

Instead, have a file or a database table to save all of the blacklisted IP addresses, and test against that in PHP, rather than on Apache's side.



来源:https://stackoverflow.com/questions/10375278/block-ip-addresses-automatically-using-php-and-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!