【发布时间】:2020-06-07 20:52:33
【问题描述】:
我在网页中有一个下载链接。我想限制这个下载网址直接点击。只有当用户来自特定的referer url时,链接才有效。
【问题讨论】:
标签: php apache redirect http-referer
我在网页中有一个下载链接。我想限制这个下载网址直接点击。只有当用户来自特定的referer url时,链接才有效。
【问题讨论】:
标签: php apache redirect http-referer
从你的 php 代码中你可以像下面那样做
<a href="download.php" >Download File</a>
你的下载.php
<?php
if($_SERVER["HTTP_REFERER"] === 'http://www.yourwebsite/yoururl') {
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename=\"" . basename($name) . "\";");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($name));
ob_clean();
flush();
readfile("your_file_path/".$name); //showing the path to the server where the file is to be download
exit;
} else {
echo "your can't download file.";
}
?>
使用.htaccess
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://www\.yourwebsite/yoururl/ [NC]
RewriteRule ^index\.php - [F]
当有人从引用站点点击指向您的 /index.php URL 的链接时,这将发送 403 Forbidden。
如果用户禁用了 HTTP 引用,浏览器将始终存在风险,但它在大多数情况下应该可以工作。
【讨论】: