如何在网站上保护可下载的pdf文件 [英] how to password protect downloadable pdf files in website

查看:152
本文介绍了如何在网站上保护可下载的pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个个人投资组合网站,我有一些pdf文件,如

 < a href =文件。 pdf>某些文件< / a> 

我不想让大家下载文件,我想让它用密码保护,我只能与我知道的人分享



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



注意:1.自从它的个人投资组合网站,它没有任何登录

2.我已经设计了HTML作为响应代码的网页



您的建议请不要以任何方式进行.htaccess?

解决方案

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



非常粗糙的模型(没有数据库,如果您熟悉dbs,请相应调整) / p>

HTML表单

 < form name =downloadid = downloadmethod =postaction =download.php> 
< input type =passwordid =passwordname =password/>
< input type =submitid =submitvalue =下载/>
< / form>

PHP(download.php)

 <?php 
//获取密码
$ pw = md5($ _ POST ['password']);

//与存储的密码进行比较
$ valid_pw = md5(您要使用的密码);

if($ pw!= $ valid_pw){
echo错误!您无权访问此文件;
} else {
header(Location:/path/to/your/file.pdf);
}
?>

注意:



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


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

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

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

解决方案

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.

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

HTML Form

<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 (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");
          }
?>

NOTES:

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.

这篇关于如何在网站上保护可下载的pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆