We need to track some PDF files we are allowing visitors to download from our website.

The files are listed out on a page that requires them to be logged in to access them. The download page tracks the visitor with sessions after they've logged to get into the download area. That login/access process is working so far using PHP and a MySQL database.

What I need to learn to do now is to be able to have an email sent out to a member of our sales/marketing department each time someone downloads a PDF file. They need to know the person's name, email, and the name of the file being downloaded, so they can followup with the visitor to see if they need additional help or information about the downloaded file.

I assume I'll need to process this through some type of a form and action file (like process_download.php) so that the database is updated with the file name(s) they're downloading, and their name and email are processed to send an email, and then redirects the visitor to a new page or tab for the PDF to be displayed so they can save it (download locally).

Does anyone know how I can make this all happen on my download page? I am not sure how to pass-on the original login information from the database (fullname and email address), and add the file name being downloaded to the PHP (mail) function. You probably guessed that I am new to PHP and I'm trying to learn as I go.

Thanks in advance for any help or direction.

    Sounds like you're close. Like you said you can create a "process_downloads.php" where you can store the info into your DB, send your email and then redirect to the appropriate page.

    As far as keeping their log in credentials, you can store them in a session and access them later from that session to store into your DB when you reach process_downloads.php.

    
    $c = array('user','email_address');
    
    $_SESSION['creds'] = $c;
    
    

      You could structure the link to the PDF that they're downloading to actually point to a PHP script. It will mail out the necessary details, and then push the PDF file to the user.

        I'd recommend writing a script for these downloads and calling it something like download.php. Then, users can download a file by visiting download.php?f=name_of_file.pdf

        download.php might look something like this:

        if (!isset($_GET["f"])) {
          die("no file specified");
        }
        //IMPORTANT:  You should make sure the specified file doesn't refer to any sensitive documents
        // you should probably screen $_GET['f'] for any slashes or backslashes
        // ideally, you might check it against a particular directory or list of valid files
        $requested_file = $_GET["f"];
        if (strstr($requested_file, "/") !== FALSE) { // or you could use DIRECTORY_SEPARATOR
          die("*nix path in filename!");
        }
        if (strstr($requested_file, "\\") !== FALSE) {
          die("windows path in filename!");
        }
        
        // you should probably put your PDFs in a location outside of your web root
        // to prevent sneaky folks from just downloading them directly
        // NOTE the trailing slash
        define("DOWNLOAD_DIRECTORY", "/path/to/pdfs/");
        
        $requested_file_path = DOWNLOAD_DIRECTORY . $requested_file
        if (!is_readable($requested_file_path)) {
          die("File not found!");
        }
        
        // at this point, the file exists and is readable, and you should send your
        // notifications
        
        // YOUR NOTIFICATION CODE HERE
        
        
        // We'll be outputting a PDF
        header('Content-type: application/pdf');
        // We will suggest a filename corresponding to the requested filename
        header('Content-Disposition: attachment; filename="' . $requested_file . '"');
        // It's good practice to tell the client software how big the file is:
        header("Content-Length: " . filesize($requested_file_path));
        // The PDF source is in original.pdf
        readfile($requested_file_path);
        
        

        I may have left something out, but most of the basic steps are here I think.

          Write a Reply...