Results 1 to 3 of 3

Thread: PHP - Register User Before Downloading File

  1. #1
    Join Date
    Dec 2011

    PHP - Register User Before Downloading File

    I would like to do the following:

    • Provide PDF documents to visitors
    • Require visitors to provide email address
    • Prevent visitors from directly access download link. If they did not provide their email address, redirect them to do so.

    I am not sure how to do this. Should I place the PDF's outside of the webroot directory? I am using Lighttpd on Debian.

    Almost all the websites I see have this functionality implemented.

    Thank you for any help.

  2. #2
    Join Date
    Aug 2004
    You can place the PDF outside of the web root directory tree, then have them submit their email via a form input. The form-handler it calls would then process the email address as desired and then serve up the PDF, e.g.:
    PHP Code:
    if(!empty($_POST['email'])) {
    // save email in DB or whatever you want to do with it
    header("Content-Type:  application/pdf");
    // make sure no extraneous bytes get sent
    else {
    // whatever you want to do if email not supplied
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Dec 2011



    First off, thank you very much for your informative response.

    When learning something, it is hard to articulate what exactly you are looking for and what you need help with.

    Thank you for your script and not the normal response of "google it or it has already been answered"

    Here is what I have:

    $errors = array();
    if(isset($_POST['email']) && !empty($_POST['email'])) 
       $_REQUEST['email'] = trim($_REQUEST['email']);
       if(substr_count($_POST['email'],"@") != 1 || stristr($_POST['email']," "))
       		$errors[] = "Email address is invalid";
       		foreach($errors as $value){print "$value<br>";}
       // save email in DB or whatever you want to do with it 
       header("Content-Type:  application/pdf");
       header('Content-Disposition: attachment; filename="downloaded.pdf"');
       exit; // make sure no extraneous bytes get sent 
    else { 
       // whatever you want to do if email not supplied 
    I can download the file via the form. However, I can also go via the URL and download the file.

    How can I prevent direct access to the PDF, or am I doing something wrong?

    Thank you.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center