The definition of hotlinking
Hotlinking is when content that is not hosted on your own server is served to end users on your own ad-bearing, profit-making page — using technical means to bypass the other site's monetized landing page. It is often small, unknown sites stealing the URLs of big, established ones (for music, images, software downloads) and embedding them in their own pages, effectively stealing the big site's storage and bandwidth.
Why hotlinking happens
A complete web page is not delivered to the client in one shot. When you request a page full of images and other assets, the first HTTP response carries the page's text; the browser then parses it, discovers the images, and sends further HTTP requests for each of them before placing them into the page. A single complete page may need many HTTP requests.
This mechanism creates the hotlinking problem: if a site doesn't host the assets its pages refer to (say, images), it can simply point those references at another site. A site with no resources of its own gets to display rich content using someone else's bandwidth, most visitors never notice, and the site being leeched pays the cost. Bad actors do it to bulk up their content without paying for hosting — it harms the origin site's legitimate interests and adds load to its servers.
How anti-hotlink protection works
To implement anti-hotlinking you first need to understand how hotlinking works, and that means the HTTP protocol. HTTP has a header field called referer, a URL indicating which page linked to the current page or file. In other words, with referer a site can detect which page a request comes from — and for a resource file, track the address of the page displaying it. Once you can track the source, the technical handling is easy: if the source is not your own site, block the request or return a designated page/image instead.
If you want anti-hotlink protection on your own site, the approach differs by server. On Apache, the built-in URL Rewrite feature handles it easily: check the referer, and if it comes from another site, redirect to a designated image or page.
On IIS you need a third-party plugin — the commonly used product is ISAPI_Rewrite, which provides Apache-like anti-hotlink features. For forums, "login verification" is another way to block hotlinking.
In IE or Firefox you can use the HttpWatch packet sniffer to inspect a page's header information, which includes the referer field showing where the user came from. Below is a PHP example simulating an anti-hotlink check that only allows referrals from the site itself:
<?php
if (isset($_SERVER['HTTP_REFERER'])) {
// Check whether the referring URL starts with our own domain; if so, it's not hotlinking
if (strpos($_SERVER['HTTP_REFERER'], 'http://www.example.com') == 0) {
echo "You arrived via another page on this site";
} else {
echo "This site forbids hotlinking";
}
} else {
echo "You arrived by typing the URL directly or similar";
}
Since the referer value is extremely easy to forge, the method above is only a simple anti-hotlink measure.
Countermeasures, in three points
- Reject empty referers (not recommended — privacy-mode browsers and HTTPS-to-HTTP jumps produce empty referers)
- Rotating signed addresses (lighttpd signs by validity window, nginx by md5)
- Login verification (assets only served after logging into an account)
A fourth option is to borrow the idea of CSRF tokens.
Frontend script approach
Implementation:
function showImg(url) {
var frameid = 'frameimg' + Math.random();
window.img = '<img id="img" src=\'' + url + '?' + Math.random() + '\' /><script>window.onload = function() { parent.document.getElementById(\'' + frameid + '\').height = document.getElementById(\'img\').height+\'px\'; }<' + '/script>';
document.write('<iframe id="' + frameid + '" src="javascript:parent.img;" frameBorder="0" scrolling="no" width="100%"></iframe>');
}
Usage:
showImg('image-url');
Backend proxy
So how do you actually bypass anti-hotlink protection and fetch the resource you need?
PHP in practice
Normally, if you embed a Baidu image directly on your own site — the embed fails! So how do you handle it with curl? After all, you need to crawl the necessary information:
$url = "http://c.hiphotos.baidu.com/image/w%3D210/sign=ed30880babec8a13141a50e1c7029157/d52a2834349b033be1a9503e17ce36d3d539bd35.jpg";
$dir = pathinfo($url);
$host = $dir['dirname'];
$refer = $host . '/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_REFERER, $refer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
header("Content-type: image/jpeg");
print($data);
Because the script prints the image directly, the result displays as an image.
Python in practice
Flask has several ways to respond to a request:
render_templatereturns a template filejsonifyreturns JSON dataResponsecustomizes the returned data and its type
When the server needs to return an image or files of various formats, you use Response for a custom response.
Comment: returning base64 data streams from the backend wastes bandwidth — somewhat self-defeating.
The CDN approach
Find an open-source image host and hotlink freely from there.
Quantum Responses (1)