如何密码保护网站中可下载的pdf文件

2022-01-02 00:00:00 download pdf password-protection php html

我有一个个人作品集网站,我有一些 pdf 文件,例如

I am having a personal portfolio website and i have a some pdf files like

<a href="file.pdf">Some file</a>

我不想让每个人都下载这个文件,我想让它用密码保护它,这样我就可以只和我认识的人分享

I dont want everyone to download the file, and i want it to protect it with password,so that i can share it with only the people i know

只有提供正确密码的人才能下载我的文件

where only the person who gives the correct password is able to download my file

注意:1. 由于是个人作品集网站,所以没有任何登录"
2. 我用HTML设计了网页作为响应式代码

Note: 1. since its a personal portfolio website it do not have any "LOGIN's"
2. I have designed the webpage with HTML as a responsive code

请提供您的建议,没有 .htaccess 的任何方法??

Your suggestions please, any way of doing it without .htaccess ??

推荐答案

使用 MySQL 或 MySQLite - 根据您的偏好 - 并将 PDF 链接存储在数据库中.然后使用诸如download.php 之类的脚本.在数据库中存储文件的密码,并要求用户在下载文件之前输入密码.如果您不熟悉数据库,您可以用 PHP 完成所有工作.

Use MySQL or MySQLite - depending on your preference - and store the link to the PDF in the database. Then use a script such as download.php. Store a password for the file in the database and require the user to enter it before the file is downloaded. If you're unfamiliar with databases you COULD do it all in PHP.

一个非常粗糙的模型(没有数据库,如果你熟悉dbs,请相应调整)

A VERY rough mockup (Without a database, if you are familiar with dbs, adjust accordingly)

HTML 表单

<form name="download" id="download" method="post" action="download.php">
  <input type="password" id="password" name="password" />
  <input type="submit" id="submit" value="Download" />
</form>

PHP (下载.php)

PHP (download.php)

<?php
     // Get the password
          $pw = md5($_POST['password']);

     // Compare against the stored password
          $valid_pw = md5("your password you want to use");

          if($pw != $valid_pw){
               echo "Error! You do not have access to this file";
          }else{
               header("Location: /path/to/your/file.pdf");
          }
?>

注意:

我使用了一种非常基本的密码加密方法.如果这是我的应用程序,我会研究更好的方法,但为了简洁和易于理解,我使用了一个简单的 md5() 哈希比较.

I used an extremely basic method of encrypting the password. I would research better methods if this was my application but for the sake of brevity and ease of understanding, I used a simple md5() hash comparison.

相关文章