Skip to content
Leonidov.DEV

Hotlink Protection in PHP: I Say No

Security
Hotlink Protection in PHP: I Say No

“Put a check in the theme so other sites stop pulling our images.”

I get this request a few times a year. I say no. Not because the code is hard. Because it runs too late.

Hotlink protection in PHP is a check that never sees the request it is meant to stop.

Where hotlink protection in PHP breaks

A browser opens https://example.com/uploads/2026/05/photo.jpg. nginx finds the file. Sends it.

PHP does not start. WordPress does not load. My function does not exist at that moment.

This is not about WordPress. Any app that serves static files through the web server has the same gap.

Some hosts push images through PHP. Then the check runs. And the price goes up: to say no to one image, the server boots the engine, the plugins and the database. A 403 that costs a full bootstrap is not protection. It is a second bill for the same traffic.

Who should answer

The rule goes where the request is cheap.

  1. Cloudflare Hotlink Protection – one toggle at the edge, no code.
  2. nginx: valid_referers none blocked server_names *.example.com; and if ($invalid_referer) { return 403; }.
  3. Apache and LiteSpeed: RewriteCond %{HTTP_REFERER} with the [F] flag.

Three rules, ten minutes each.

All of them let an empty referer through. That is the correct default. Email clients, RSS readers, privacy browsers and social previews arrive with nothing. Block the empty referer and you break more traffic than you save.

Why plugins still do it

Some hotlink plugins write rules into .htaccess. That works, until the site moves to nginx and the rule stays behind in a file nobody reads.

Others check the referer in PHP. That is the late check again, with a menu page on top.

A third group hides the real file path behind a signature. Different task, different price, and it breaks caching.

None of these are stupid. They are just solving the problem one layer below the place where it is cheap.

What I still write in PHP

There is a job the perimeter cannot do. Deciding who is on the other end.

A scraper with a forged referer. A crawler that calls itself Googlebot without a matching PTR record. An integration that broke after a deploy and now sends the wrong header.

Referer is one signal. A client can fake it in one line. So it cannot be the only check.

For WordPress that layer is BotBlocker. Its firewall starts before WordPress finishes loading, reads headers and User-Agent signatures, and writes the decision to a log I can read.

Perimeter cuts volume. The plugin answers who came in.

How I explain it on a call

Three sentences.

  1. The server sends the image before your code starts, so your code cannot refuse it.
  2. The rule belongs to the layer that answers the request: CDN, nginx, Apache.
  3. The plugin is for knowing who the client is, not for saving traffic.

Usually enough. If the client wants a number, I open the access log and sort by referer and bytes. The top three foreign hosts explain most of the bill.

Then I run four checks. Own referer returns 200. Foreign referer returns 403. Empty referer returns 200 unless we decided otherwise. Direct file URL in a private window still loads.

The fourth one catches the client that sends nothing at all. An allowlist entry fixes it in five minutes, and the rule keeps working for everyone else.

Turning the rule off takes two minutes and brings the topic back in a month.

What this teaches

Ask which layer answers the request before writing code for it.

Hotlinks, bots, cache headers, redirects. Same question every time. The answer rarely sounds like “in the theme”.

Related: WordPress security for the perimeter my team builds, and skills and technologies for the rest of the stack I work with. If your images are already being pulled, start with the log. BotBlocker handles the client decisions after the rule is in place.

Background: Cloudflare Hotlink Protection, nginx referer module, Apache mod_rewrite.