防盗链(Hotlink Protection)是指网站为了保护自己的资源不被其他网站盗用而采取的一种技术手段。通常,网站会检查HTTP请求的Referer头部信息,如果请求不是来自自己的网站,则拒绝提供资源。
防盗链机制通过检查HTTP请求的Referer头部信息来判断请求是否合法。如果请求的Referer不是来自你的网站,则会被拒绝。
<?php
$url = 'https://example.com/protected-image.jpg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'https://your-website.com');
$response = curl_exec($ch);
curl_close($ch);
header('Content-Type: image/jpeg');
echo $response;
?><?php
session_start();
if (empty($_SESSION['token'])) {
$_SESSION['token'] = bin2hex(random_bytes(16));
}
$token = $_SESSION['token'];
$url = 'https://example.com/protected-image.jpg?token=' . $token;
?>
<img src="<?php echo $url; ?>" alt="Protected Image"><?php
$url = 'https://example.com/protected-image.jpg';
$proxy = 'http://your-proxy-server:port';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
$response = curl_exec($ch);
curl_close($ch);
header('Content-Type: image/jpeg');
echo $response;
?>请注意,突破防盗链可能涉及法律和道德问题,建议在合法合规的前提下使用相关技术。