You can see the visitor's actual IP by installing mod_cloudflare.
https://support.cloudflare.com/hc/en-us/articles/200170836-How-do-I-restore-original-visitor-IP-to-Apache-Web-Servers-
How do I restore original visitor IP to Apache Web Servers?
To restore the original visitor IP addresses to log files and web
applications running on Apache httpd web servers, you will need to
install mod_cloudflare. To install mod_cloudflare, follow the
installation steps described on our Downloads page.
After that you may block the malicious IPs on the Apache level via htaccess before any request reaches any PHP script.
Update:
The Cloudflare downloads page provides ready made packages for common
linux distributions. If your distribution is not supported you may
compile the module for your apache as described here:
https://www.cloudflare.com/resources-downloads#mod_cloudflare
Option 4: Manual Installation: Debian / Ubuntu
mod_cloudflare has a few software dependencies that need to be
installed first:
apt-get install libtool apache2-dev
Note: If you find that you are unable to install apache2-dev then you
should install:
apt-get install libtool apache2-threaded-dev
Next, you should download the mod_cloudflare source to your server:
wget https://www.cloudflare.com/static/misc/mod_cloudflare/mod_cloudflare.c
Finally, install the module. Depending on your system, the command to
run might be apxs or apxs2. So, run one of the below two commands. If
you get a "Command not found" when running one, try the other:
apxs -a -i -c mod_cloudflare.c
apxs2 -a -i -c mod_cloudflare.c