D3vil_Mind D3vil_Mind - 3 months ago 26
HTML Question

how to password protect downloadable pdf files in website


Possible Duplicate:

How to password protect an uploaded pdf in PHP




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 ??

Answer

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.