Build a custom affiliate link cloaker in PHP for Nigerian offers
Affiliate marketing across borders has quietly become one of the most practical side hustles for Australian digital entrepreneurs, especially those working with Nigerian offers like airtime top-ups, VTU platforms, and bulk SMS providers. The challenge is that raw affiliate URLs are long, ugly, and easy for visitors to manipulate before they click, which can strip your commission in seconds. A custom cloaker built in PHP solves this elegantly, and it does not require a framework or expensive tooling to get right.
Whether you are a Sydney-based freelancer testing the waters or a Melbourne growth hacker scaling a review site, the principles are identical. You write a small redirect script that swaps your branded short link for the affiliate destination, logs the click for analytics, and optionally rotates offers for split testing. The rest of this guide walks through the practical build from environment setup to deployment, with notes on Australian hosting and privacy considerations woven in.
Why link cloaking matters for affiliate campaigns
Affiliate links from Nigerian networks such as those covering MTN, Glo, Airtel, and 9mobile data bundles typically come loaded with sub-IDs, tracking tokens, and long query strings. When you share such a URL on Twitter, WhatsApp status, or a Telegram channel, two things happen. First, the link looks unprofessional, which reduces trust and click-through rates. Second, any visitor who knows what they are doing can strip the sub-ID parameter and complete the purchase directly, denying you the commission you earned.
A cloaker solves both problems by giving you a clean, brand-aligned short URL that redirects through your own server. You keep control of the redirect chain, the metadata, and the analytics. For Australian publishers especially, this matters because local audiences have become cautious about suspicious-looking domains after years of scam warnings from the ACCC. A clean cloaked link on your own domain reads as trustworthy rather than as a fly-by-night referral.
There is a secondary benefit that matters for compliance. Many Nigerian networks forbid direct bidding on brand terms or disallow certain promotional methods. A cloaker lets you vary the destination URL without changing the visible link, which keeps your campaigns flexible while staying within the rules.
Setting up your PHP environment
You do not need much to get going. Any machine running PHP 7.4 or newer will do, whether that is a MacBook in Brisbane, a Linux VPS, or a Windows workstation running WSL. The rest of the build assumes PHP 8.1 since most Australian shared hosts now default to that version. You will also need MySQL or MariaDB for storing the redirect rules and click logs.
Local development is straightforward with XAMPP, MAMP, or the built-in PHP server. If you prefer Docker, a tiny docker-compose file with php:8.1-apache and mariadb:11 services is enough. Pick whichever stack you are comfortable with because the script itself is portable and will run anywhere that speaks PHP and SQL.
On the production side, Australian readers should weigh hosting close to home against latency to Nigeria. Sydney or Melbourne data centres keep your dashboard snappy but add roughly 200 to 300 milliseconds of round-trip time when the redirect finally hands off to the affiliate network. If you are running heavy campaigns targeting Nigerian visitors, a Lagos or Johannesburg edge server may perform better, but for an Australian admin audience the local option is usually the smarter pick.
Designing the database schema
Two tables are enough for most cloakers. The first table stores the mapping between your short slug and the destination URL, along with metadata such as campaign name, network, and expiry date. The second table records each click with a timestamp, IP address, user agent, and the referring page, which is the raw material for conversion analysis later.
A minimal schema for the links table would look like this. The slug column should be unique and indexed, the destination stores the full affiliate URL, and a status flag lets you pause a campaign without deleting the row. For the clicks table, store the link ID as a foreign key, the request time as a datetime, and the IP as a string. Hashing the IP with a salt before storage is a sensible move if you want to stay on the right side of the Australian Privacy Principles, which apply whenever you collect personal data from Australian residents.
You will also want a tiny admin table with a hashed password and a session token column. Keep it simple because the goal is to ship a working tool, not to build a SaaS platform.
Writing the cloaker redirect script
The redirect logic is the heart of the build. A typical request flow looks like this: the visitor hits yourdomain.com/go/airtime, the script looks up the slug, validates the destination, logs the click, and issues a 302 redirect. A 302 is preferred over 301 because you may want to change the destination later without breaking existing links, and search engines will continue to treat the slug as your own URL.
In PHP, the entry point is a single index.php inside a /go/ subdirectory, with rewrite rules in your .htaccess file that funnel every request to that file. The script pulls the slug from the request URI, queries the database, and either performs a header redirect or returns a 404. Adding a cache layer with APCu or a flat-file lookup speeds things up and protects your database during viral spikes, which happen often when Nigerian promos trend on WhatsApp.
For campaigns that need split testing, store multiple destinations per slug and rotate them based on a hash of the visitor IP. This gives you an even distribution without exposing visitors to obvious A/B buckets that can skew results.
Adding tracking and analytics
Once the basic redirect is working, layer in analytics so you can see which campaigns actually convert. At minimum, capture the click timestamp, the slug, the user agent, and a hashed version of the IP. You can then write simple SQL queries to compute clicks per day, conversion ratio if you have postback data, and referrer breakdown.
If you want richer insight without running your own dashboard, fire a server-side event to Google Analytics 4 using the Measurement Protocol, or push the data into Plausible or Matomo self-hosted on the same server. Both tools respect the Privacy Act 1988 requirements that Australian site owners must follow, and they keep you out of trouble with ACMA guidelines around consent notices.
A small admin dashboard built with plain PHP and a handful of CSS rules will be enough to display click volume, top-performing slugs, and recent activity. Resist the urge to over-engineer because every extra dependency is another thing to maintain.
Deploying and staying compliant in Australia
Before you go live, test the redirect thoroughly across browsers and devices, including mobile networks on Telstra and Optus, since mobile traffic dominates the Nigerian affiliate space. Verify that the slug lookup is case-insensitive, that expired links return a polite 410 Gone page rather than a redirect, and that bot traffic from common crawlers does not pollute your analytics.
Hosting in Australia brings a few specific obligations. The Privacy Act 1988 applies the moment you collect any data from Australian visitors, so publish a short privacy policy that explains the IP hashing, retention period, and how someone can request deletion. If you monetise through ads on the same domain, remember that GST applies once your Australian turnover crosses the threshold, so register for an ABN and factor that into your revenue reporting.
Finally, integrate your cloaker with the rest of your marketing stack. Many Australian creators pair their cloaked affiliate links with a WhatsApp Business channel for customer support and follow-up broadcasts, which is a natural fit because WhatsApp is the dominant messaging app in Nigeria. You can deepen that workflow by following a practical walkthrough on WhatsApp Business support tactics that covers broadcast lists, quick replies, and catalogue sharing. Pairing a smart PHP cloaker with a well-tuned WhatsApp funnel turns a handful of static links into a measurable, scalable revenue engine that runs on your own infrastructure and on your own terms.