简体   繁体   中英

How i can allow users to access download link from the specific web-page with HTACCESS in php

I have a download link in a webpage. I want to restrict this download url from direct hit. Link will only work if user come from specific referer url.

From your php code your can do it like below

<a href="download.php" >Download File</a>

your download.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."; 
}
?>

using .htaccess

RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://www\.yourwebsite/yoururl/ [NC]
RewriteRule ^index\.php - [F]

This will send a 403 Forbidden when someone follows a link to your /index.php URL from the referring site.

There is always the risk that browsers won't send an HTTP referer if the user has disabled this, but it should work in most cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM