U
U
uuuu2020-04-27 00:14:50
PHP
uuuu, 2020-04-27 00:14:50

How to edit .htaccess file via PHP?

I created a bot that detects malicious IPs in PHP, but I can't edit the .htaccess file through PHP. I found information that it is better to do this through RewriteEngine, but how to do it? I have a large array of IPs that is constantly growing.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Barmunk, 2020-04-27
@uuuu

https://htaccessbook.com/block-ip-address/
choose to your taste.
But I do not advise giving access to write htaccess, there will be a clear hole. It is better to configure fail2ban to parse the next log, or use php to block ip addresses, taking them from a csv file that is being filled, for example

S
SagePtr, 2020-04-27
@SagePtr

The problem is that Apache is gluttonous, and if you are trying to protect yourself from a lot of requests with .htaccess, then most likely, these requests will already put Apache. And also, if there are a lot of addresses in the blacklist, then this will greatly slow down legitimate requests, because with each request .htaccess will have to be read and parsed, which will grow very much.
Therefore, it is necessary to block explicitly even earlier, for example, using a firewall (if you do not consider external protection methods, in which this task falls on the hoster or an intermediate link like cloudflare, and already filtered requests reach the server).
With a large volume - the black list should not be linear, because. time O (n) obviously does not suit us, checking for the presence of an address will take a lot of time, for example, ipset will help, storing marked addresses in the form of a hash table.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question