www.webdeveloper.com
Results 1 to 3 of 3

Thread: PHP - Register User Before Downloading File

  1. #1
    Join Date
    Dec 2011
    Posts
    2

    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
    Location
    Ankh-Morpork
    Posts
    19,388
    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:
    <?php
    if(!empty($_POST['email'])) {
       
    // save email in DB or whatever you want to do with it
       
    header("Content-Type:  application/pdf");
       
    readfile("../path/to/pdf/file.pdf");
       exit; 
    // make sure no extraneous bytes get sent
    }
    else {
       
    // whatever you want to do if email not supplied
    }
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Dec 2011
    Posts
    2

    Thanks

    NogDog,

    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:

    Code:
    <?php
    
    $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>";}
       		exit;
       }
       
       // save email in DB or whatever you want to do with it 
       header("Content-Type:  application/pdf");
       header('Content-Disposition: attachment; filename="downloaded.pdf"');
       readfile("../files/test.pdf"); 
       exit; // make sure no extraneous bytes get sent 
    } 
    else { 
       // whatever you want to do if email not supplied 
       exit;
    }
    ?>
    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



Recent Articles