Tinkerwell - The PHP Scratchpad

Geocoding with Cloudflare

Last updated on by

Geocoding with Cloudflare image

I like to offer Laravel Shift for "20 bucks". But $20 may be more or less depending on where you live. So if you're in a country where the value of your dollar is 50% less than USD, I offer purchasing power parity.

To determine the country you live in, I use a geocoding service. One that translates your IP address to your country. If that country is one where I offer purchasing power, you see a banner at the top to activate your discount.

Now, I've been on a quest to make the laravelshift.com as fast as possible using Cloudflare. Along the way I stumbled upon another one of their free services - geocoding. Cloudflare appends a country header to the request before forwarding to your web server.

The CF-IPCountry header is a 2 character country code. For example, US or UK. In my case, I was able to use this header directly. Then map the code to a country name to check for purchasing power.

This allowed me to do two things. First, it allowed me to remove hundreds of lines of code. Second, it allowed me to cache the page.

Since the page previously had logic to check the request headers, it was too dynamic to cache. Instead, I sent an API call to check the country after the page load.

All of this was not only more code, but an additional, albeit lightweight, request.

But since this header is available at the Cloudflare level, I was actually able to create a transformation rule. Within it I could check the country code and append a query string to the request.

This allows me to cache two versions of the page. One for the default page display, and another displaying the purchasing power banner.

Again, since this is opt in, your location header is double checked before activating the discount.

If you're interested in seeing the full code refactor, I livestreamed this for my Confident Laravel testing course..

If you're interested in making your page load super fast, check out my video course Fast Laravel.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Bacancy

Outsource a dedicated Laravel developer for $3,200/month. With over a decade of experience in Laravel development, we deliver fast, high-quality, and cost-effective solutions at affordable rates.

Visit Bacancy
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
Speeding Up Laravel News With Cloudflare image

Speeding Up Laravel News With Cloudflare

Read article
Livewire 4 Support in Laravel VS Code Extension v1.4.3 image

Livewire 4 Support in Laravel VS Code Extension v1.4.3

Read article
Fair Queue Distribution with Laravel Balanced Queue image

Fair Queue Distribution with Laravel Balanced Queue

Read article
Migrating Laravel News from Laravel Forge to Cloud image

Migrating Laravel News from Laravel Forge to Cloud

Read article
Laravel News Is the Live Stream Partner for Laracon EU 2026 image

Laravel News Is the Live Stream Partner for Laracon EU 2026

Read article
Query Builder Expression Aliases in Laravel 12.48 image

Query Builder Expression Aliases in Laravel 12.48

Read article