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
Laravel Code Review

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

Visit Laravel Code Review
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
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
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
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
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
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 →
FluentVox: AI Text-to-Speech for Laravel with Voice Cloning and GPU Acceleration image

FluentVox: AI Text-to-Speech for Laravel with Voice Cloning and GPU Acceleration

Read article
Laravel Boost v1.8.10 Released With New AI Prompts and Livewire v4 Support image

Laravel Boost v1.8.10 Released With New AI Prompts and Livewire v4 Support

Read article
Statamic 6 Beta Now Available image

Statamic 6 Beta Now Available

Read article
Everything new in Livewire 4 image

Everything new in Livewire 4

Read article
Cache Without Overlapping in Laravel 12.47.0 image

Cache Without Overlapping in Laravel 12.47.0

Read article
Going Real-Time with Reverb - Laravel In Practice EP17 image

Going Real-Time with Reverb - Laravel In Practice EP17

Read article