不允许用户从url访问文档


Dnt allow user to access document from url

我希望登录用户可以访问PDF文件,匿名用户不能从浏览器访问文件,如www.domain.com/pdf/name.pdf

我的pdf文件损坏了。当点击下载时失败。

我已经创建了pdf文件夹,保存我所有的pdf。我有html代码

<ul>
<li>
    <a href="/check.php" target="_blank">Test</a>
</li>
</ul>

check.php文件
    if($_SESSION[login]==true){
    $file_url = 'http://domainname.com/pdf/example.pdf';
    $filename='example.pdf';
    header("Content-Disposition: attachment; filename=" . urlencode($filename));   
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Description: File Transfer");            
    header("Content-Length: " . filesize($file_url));
$fp = fopen($file_url, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush(); // this is essential for large downloads
} 
fclose($fp);
}

ht。访问

RewriteEngine on
RewriteRule .* check.php

只需在php文件中添加如下函数:

function protect_page(){
if(logged_in()===false){
    header('Location: protected.php');
    exit();
}
}

执行

检查用户是否登录
$_SESSION[login]==true

把上面的protect_page()函数写在有pdf链接的页面上。

你可以走了