2

我想制作一个具有 pdf 下载选项的网页,但我希望它受到密码保护,即如果有人点击该链接,他必须输入用户名和密码,并且如果他直接打开链接“www.example.com/~folder_name /abc.pdf" 然后服务器首先要求输入密码然后允许下载

编辑:我希望用户在浏览器中查看文件,而不是强制下载这是我的代码

<?php
    /* authentication script goes here*/
    $file = 'http://example.com/folder_name/abc.pdf';

    //header('Content-Description: File Transfer');
    header('Content-Type: application/pdf');
    header('Content-Disposition: inline; filename=' . basename($file));
    header('Content-Transfer-Encoding: binary');
    //header('Expires: 0');
    //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    //header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    header('Accept-Ranges: bytes');
    @readfile($file);

?>

但是这段代码没有在我的浏览器中打开 pdf。我不希望代码依赖于浏览器使用的 pdf 插件

4

2 回答 2

1

您可以.htaccess在设置了下载的 Web 文件夹中创建一个文件,这样在任何人都可以进入域之前,他们必须输入正确的用户名和密码才能进入。

这是我在设置自己的博客时使用的博客文章,但本质上您的 .htaccess 文件将如下所示:

AuthType Basic
AuthName "restricted area"
AuthUserFile /path/to/file/directory-you-want-to-protect/.htpasswd
require valid-user

您还需要创建一个.htpasswd文件,您可以在其中放置用户名和密码。密码需要使用 MD5 哈希加密,但您可以使用他在博客中链接到的生成器。希望这可以帮助。

于 2013-09-04T05:32:10.497 回答
0

您仍然可以使用.htaccess不让任何人直接下载您的文档并保护文档的链接。

.htaccess 可能是这样的

RewriteRule ^([A-Za-z0-9-]+).pdf$ index.php [L,QSA]

您可以为此使用 php。

有人这样想

<?php

    //here you authenticate user with your script

    //and then let the user download it
    if (!isset($_SESSION['authenticated']))
    {
       header('Location: http://www.example.com/');
       exit;
    }
    $file = 'www.example.com/~folder_name/abc.pdf';

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
?>
于 2013-09-04T05:47:20.210 回答