I've got a bunch of PDF files that contain sensitive data, in a previous thread I asked how to protect these files from unauthorized access, but still allowing access to authorized users. The solution offered was to use a .htaccess file.

So I've done some research into it, and added a .htaccess file to the protected directory. Now when an authorized user logs into my site through my PHP/MySQL log-in page, they get to a directory of PDFs and when they select a PDF to download, they are prompted with a browser-driven log-in box.

So I've been looking for a solution to prevent the need for a double log-in. So far the solutions that I've found are:

  1. By logging in using a plain text password, as shown on this site. Like http://username:password@www.website.com/directory/

  2. I've been reading about a script that I think addresses this problem, called pajamas.

So my question is: How do I avoid the double log-in, and still keep my data safe? The first link says that there are scripts to avoid the double log-in, and I'm assuming that "pajamas" is one of those scripts. Does anyone know of any others? or is there a better way altogether?

If it helps, I'm on a Unix, Apache web-server, using a hosting company, so I don't have the ability to install any extensions. But they may already have something installed. I just don't know what to look for.

Thanks for all your help.

    Conceptually, you could put your PDF files into a private folder somewhere inaccessible from the web, then serve them up from a PHP script:

    if ($access_granted)
    {
        header('Content-type: application/pdf;');
        readfile($path_to_pdf);
    }
    

    Then you can avoid .htaccess schemes altogether. See readfile()

      Thanks for the reply, I need to learn more about setting headers. I've been trying to do some research into it, but I still just don't get how they work.

      Is there a way to set the file name? In Firefox on a Mac, when I open the script, it starts to download the file. But it names the file with the script's name. I named the script "display.php", and that's exactly what the pdf is named after downloading. So if I double-click on it, it opens Dreamweaver, and it causes DW to crash. When I change the extension to pdf, it works fine. But my clients aren't going to know to do that. And if they download more than 1 pdf, there not going to know which one is which, unless I can get it to download with the original pdf name.

      Thanks again for the help, I really appreciate it.

        I found this thread that solves the filename problem, but it always gives a dialog box that gives the option of opening or saving. I would ultimately like the PDFs to always display in the browser if possible. Since these files contain sensitive info, I really don't want copies of them floating around.

        If I'm not mistaken displaying PDFs in the browser depends a lot on the configuration of the browser and whether or not it's sync'd up with Acrobat Reader. Is that right, or is there a way to force it to display in the browser window?

        Thanks for any advice.

          You can set the name in the header:

          header('Content-Disposition: attachment; filename="'.basename($path_to_pdf).'"');
          

          Theres some examples in the manual:
          http://www.php.net/header

            Thanks. I looked through the header examples, and found that what I want to do works with:

            header('Content-Disposition: inline; filename="'.$filename.'"');

            Is there a way to limit what a user can do with the PDF once it's open in their browser? I want to allow them to print the file, but not to save it to their local drive.

            Thanks again.

              You have to use Adobe's built-in PDF security features for things like that, although anything you display on the client is as good as stolen already.

                Write a Reply...