CloudFlare & CloudFront API class library

Posted on May 11, 2021

When you use CloudFlare or CloudFront as a reverse proxy service, you can retrieve a visitor’s IP address and country code from their special offered environment variables.

In this article, I’ll show you how to make use of their services.

Cloudflare

Cloudflare provides the various client information via HTTP request headers. For example, an IP address can be retrieved from CF-Connecting-IP and the country code from CF-IPCountry.

So here is a sample of the API class library for Cloudflare.

The key point here is that the above library should be named as class-zcloudflare.php and typically placed under the directory named /wp-content/uploads/ip-location-block/apis/zcloudflare/.

You might wonder why z + cloudflare?

Well, it’s just a matter of convenience for this plugin to give it the highest priority!

CloudFront

CloudFront also provides the client information via HTTP response headers. So the code is almost the same as Cloudflare. You can find it at gdarko/class-zcloudfront.php.

Pros and Cons

The advantage of using these libraries is definitely the response time that
is important especially for the front-end. It’s almost zero.

Response time of each API

On the other hand, the disadvantage is any IP address can not be retrieved on the “Search” tab. In this case, you should just select another API 😉.

Search tab

Leave the first comment