【问题标题】:How can I show a non-public PDF file in browser?如何在浏览器中显示非公开 PDF 文件?
【发布时间】:2019-11-24 10:58:27
【问题描述】:

我有点问题。我目前正在构建一项服务,人们可以在 WordPress 中签署 PDF。为此,我使用了一个模板,该模板位于 WordPress 的公共上传文件夹中。

所以在用户签名之前,他可以在浏览器中通过https://www.my-site.de/wp-content/uploads/template.pdf轻松查看此模板。

在他签名后,我将创建一个包含所有相关信息的模板副本并将其移动到子文件夹signed-templates

$pdf->Output( 'F', wp_upload_dir()['basedir'] . '/signed-templates/123456.pdf' );

在签名模板文件夹中,我有一个 .htaccess 文件,其中包含以下内容,以防止通过浏览器进行公共访问:

deny from all

现在我来谈谈我的问题。第一个用户签名并保存在上面的文件夹中。现在第二个用户需要查看并会签它。

在他会签之前,我将通过链接在浏览器中向他展示该文件。那么我该怎么做呢?我的意思是,由于.htaccess 文件,似乎无法访问该 URL,但这是防止个人信息泄露所必需的。

每个文件都有一个唯一的 id,这个 id 保存在我的数据库中。所以我拥有的唯一信息是文件名123456.pdf 和它的路径。

也许可以选择通过 AJAX 显示它?这将是我唯一的想法。

【问题讨论】:

  • 您想要使用 PHP 和相关代码的编码答案,还是正在寻找 WordPress 功能性答案?
  • WordPress 是用 PHP 编写的,所以 PHP 也可以工作。如果你只知道 PHP,我可以使用 AJAX 之类的 WordPress 函数重写它。重要的是让它发挥作用。
  • 如果你不是 PHP 核心程序员,我的回答可能对你来说太复杂了
  • 可以使用 Ajax,但我正在运行的概念是使用 PHP 接口文件,以便浏览器永远不会看到 PDF 的真正主页

标签: php ajax wordpress pdf


【解决方案1】:

我已在各种网站上成功使用的方法是:

1) 创建一个将加载数据的 PHP 文件 (PDF),

2) PHP 被赋予某种形式的身份验证(nonce key 值或类似的数据库驱动的唯一值)

3) PHP 文件接受身份验证(例如来自电子邮件的链接),然后从受保护的目录加载相应的 PDF。

.htaccess 阻止 浏览器 访问文件,但由于 PHP 在本地文件系统上,它仍然可以加载 PDF 文件并将其作为匿名“签名”直接输出到客户端浏览器.php”文件。用户 2 永远不会知道 PDF 实际存储在服务器上的哪个位置(即使他们知道了,他们也无法直接访问它)。


示例

存储PDF的目录是/files/signed
PHP文件的目录是/countersign.php
数据库将有id (INT(8) A_I ) | filename (VARCHAR(255)) | nonce_code (VARCHAR(255) UNIQUE)

然后,一旦用户 1 签名 - 生成一个 Nonce(又名 PRNG)代码并通过电子邮件发送给用户 2。

示例数据库行:

  id    |   filename   |               nonce_code
===========================================================
   4    |  123456.pdf  | AB463747CH567BC5456AB45023DBC36214

用户 2 收到一封带有链接的电子邮件:

https://www.my-site.de/countersign.php?id=AB463747CF567BC5456AB45023DBC36214

用户点击链接; PHP 将搜索该链接:

注意:下面的代码块包含相当多的汇总和伪代码 - 这是为了说明方法,而不是适合您标准的确切方法

$id = $_GET['id']; 
/***
 * DB check value is valid and do basic cleaning such as with a Hex checker. 
 * In this  example the nonce value is a Hex string but you can use your own system
 ***/
$dataBase = new dataBaseClass();

/***
 * Check database for this value and return the PDF string. 
 ***/ 
$result = $dataBase->prepare("SELECT filename FROM table WHERE nonce_code = :ncode ");
$result->execute([':ncode' => $id]);


/***
 * Now PHP loads the retrieved filename from the signed directory, 
 * and outputs it to the browser 
 ***/
$f = $result->fetch();
$file = wp_upload_dir()['basedir'] . '/signed-templates/'.'$f['filename'];
if(is_file($file)){
    /***
     * You should have a security, check that the file called is a PDF type 
     * (not shown here)
     ***/
    $output = file_get_contents(urlencode($file));
    if(!empty($output)){
        /***
         * PHP tells the browser it will act as if it is a PDF file:
         ***/
       header("Content-type: application/pdf");
       header("Content-Disposition: inline; filename=filename.pdf");

       /***
        * The PDF will only be accessible to the user who loads the
        * page with the correct nonce code
        ***/
       print $output;  
    }
    else {
         // File error.
    }
}
else {
    // no file found.
    /***
     * Add a delay to throttle spam attempts to discover files
     * (various other practises can be employed depending on the required
     * security level)
     ***/
    sleep(2);
}

PDF 被会签后,您可以更新数据库以清除 nonce_code 值。

【讨论】:

  • 哇,很酷的方法。非常感谢马丁,我现在就试着让它工作。当我得到它或当我有一些问题时,我会告诉你!
  • @Mr.Jo 你可以使用mod_rewrite 让它变得非常聪明,如果你愿意的话:https://www.my-site.de/countersign/AB463747CF567BC5456AB45023DBC36214,同样,还有很大的潜力可以让它变得更聪明:-)
  • 这是我的想法,所以我会尽力的:)
猜你喜欢
  • 1970-01-01
  • 2019-09-08
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
  • 2021-03-15
  • 1970-01-01
  • 2023-03-15
相关资源
最近更新 更多