【问题标题】:How i can allow users to access download link from the specific web-page with HTACCESS in php我如何允许用户在 php 中使用 HTACCESS 从特定网页访问下载链接
【发布时间】:2020-06-07 20:52:33
【问题描述】:

我在网页中有一个下载链接。我想限制这个下载网址直接点击。只有当用户来自特定的referer url时,链接才有效。

【问题讨论】:

    标签: php apache redirect http-referer


    【解决方案1】:

    从你的 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 引用,浏览器将始终存在风险,但它在大多数情况下应该可以工作。

    【讨论】:

    • 上述解决方案对我不起作用。我只需要使用 htaccess 来完成这项工作(不使用 $_SERVER["HTTP_REFERER"])
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    相关资源
    最近更新 更多