phppdf.htaccesshttp-redirecturl-rewriting

Redirect files to simple form before downloading


I am trying to redirect all links to any PDF file in my site to a page with a form in it that collects user info before they can proceed to download/view the PDF.

E.g.

I want to redirect *.pdf files in a web site to request.php?file=name_of_pdf_being_redirected where request.php is the page with the form on it asking for a few details before proceeding.

All PDFs in the site are held inside /pdf folder.

Any ideas?

I'm using Apache on the server.


Solution

  • You can try this:

        if ( isset( $_SESSION[ 'OK_TO_DOWNLOAD' ] ) == false )
        {
            header( "Location: must_fill_this_first.php" );
            exit( 0 );
        }
    
        header( "Content-type: application/pdf" );
        // double check the above, google it as i am not sure
    
        echo file_get_contents( 'some_directory_inaccessible_thru_www/' . $_GET[ 'pdf_name' ] );
        // ideally a binary-safe function needs to be used above
    

    This is a tried and tested technique I used on a website. The code example is a draft outline and needs refinement.