Laravel Cloud is here! Zero-config managed infrastructure for Laravel apps. Deploy now.

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
Laravel Cloud

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

Visit Laravel Cloud
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
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
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
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Shift logo

Shift

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

Shift
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
Tinkerwell logo

Tinkerwell

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

Tinkerwell
PhpStorm logo

PhpStorm

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

PhpStorm

The latest

View all →
Typed Objects for Eloquent with Expressive image

Typed Objects for Eloquent with Expressive

Read article
Malware Blocking and Dependency Policies in Composer 2.10 image

Malware Blocking and Dependency Policies in Composer 2.10

Read article
Aegis for Laravel: Scaffolding and Validation Helpers for Value Objects image

Aegis for Laravel: Scaffolding and Validation Helpers for Value Objects

Read article
Playa: Cookie-Based Temporary Players for Laravel image

Playa: Cookie-Based Temporary Players for Laravel

Read article
Scheduler Attributes and Listener Discovery Control in Laravel 13.12.0 image

Scheduler Attributes and Listener Discovery Control in Laravel 13.12.0

Read article
The PHP Foundation Launches an Ecosystem Security Team image

The PHP Foundation Launches an Ecosystem Security Team

Read article