#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    288
    Rep Power
    0

    Prevent direct file access but enable it from a link


    Hi everyone, my title may seem weird, but here's some details about what I'm trying to achieve and if it's possible.

    I have a PHP file, let's say page.php, that I let users authenticate and I use sessions to manage their authentication, so only authenticated people can see what's inside that page.

    On that page, there's a link, a simple link, that opens a PDF file on a separate window. So that PDF is 'protected' in a way, sort of.

    But as you can see, it's not 'really' protected, someone who would have the exact URL of the PDF file could copy-paste it and access the file directly.

    What I want to achieve is let people who are authenticated click on the link and open the PDF file on a separate window BUT prevent the PDF from being accessed directly from it's URL.

    I want to prevent direct access to those PDF from people directly typing or copying the address without having clicked on the link when authenticated.

    I know it's possible for an included file, but a separate PDF, is it?

    Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,322
    Rep Power
    630
    Instead of a simple link use a link to another page that outputs the PDF file using headers. Re-verify the authentication on that page but make sure you do not output anything (not even white space) before the headers.

    PHP Code:
    header("Content-type:application/pdf");

    header("Content-Disposition:attachment;filename='somefile.pdf'");

    readfile("somefile.pdf"); 
    Last edited by gw1500se; September 5th, 2017 at 09:00 AM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2017
    Location
    Bulgaria
    Posts
    15
    Rep Power
    0
    Search in Google for this and you will find what you need: php protect download pdf

IMN logo majestic logo threadwatch logo seochat tools logo