I hope someone can help. I want to require users to fill out a form before they can access PDFs. The PDFs are stored outside the web directory.
The logic is like this:
--a user clicks on a link to a PDF
--they redirected to a page and are asked to fill out a form
--if they fill out the form, a session variable gets set, an email gets sent to me telling me who accessed the PDF, and they are provided with a new link to the PDF which will allow them to download it
--as long as they don't close the browser they can go back and download more PDFs without having to fill out the form again
The problem:
--the client wants to receive an email for each and every PDF download, even if a user's session is still active. They don't mind asking the user to refill out the form for each PDF download.
The solution I've tried but failed at:
--I have tried to destroy the session after they download a PDF, but no matter what I can't destroy it--I've tried these, some give errors, so just don't do anything:
(session_unregister();
session_unset();
session_destroy();
$SESSION = array();
unset($SESSION['ok']);
Here is the download script:
<?php
session_start();
header("Cache-control: private"); // IE 6 Fix.
$SESSION['name'] = $HTTP_GET_VARS["name"];
$name = $SESSION['name'];
//// if user filled out form, go to pdf
if(isset($_SESSION['ok'])){
$path = '/home/xxx/pdfs/'."$name" . ".pdf";
header('Content-Type: application/pdf');
$fp = fopen($path, 'rb'); //r means read only and b means binary
fpassthru($fp); // this will send the file to the browser
unset($HTTP_SESSION_VARS['name']);
//I've tried to destroy the session here
exit;
//and here
}else{
header("Location: http://www.problem.com/form.php"); //go to the form
exit;
}
//and here
?>
When the user fills out the form these things happen:
(1) a session variable gets set ($_SESSION['ok'])
(2) an email is sent to me containing the user info and the name of the PDF they downloaded
(3) the user sees a link to the PDF which takes them back to pdfdl.php
It works great except the client now wants to
Here is the form:
<?php
session_start();
header("Cache-control: private"); // IE 6 Fix.
$name = $_SESSION['name'];
$message = NULL;
if($_POST['submit']){
if(empty($_POST['fname'])){
$fname = FALSE;
$message .= "A first name is required.<br>";
}else{
$fname = $_POST['fname'];
}
if(empty($_POST['lname'])){
$lname = FALSE;
$message .= "A last name is required.<br>";
}else{
$lname = $_POST['lname'];
}
///////if everything's ok
if ($fname && $lname){
$_SESSION['ok'] = 'ok';
// Send an email
$body = "PDF Downloaded: ".$name."\n";
$body .= "First Name: ".$fname."\n";
$body .= "Last Name: ".$lname."\n";
$headers = "From: [email]web@americanvalues.org[/email]\n";
mail(xxx@xxx.org, 'PDF Download', $body, $headers);
$message2 = "Please click <a href=\"pdf_dl.php?name=$name\">here</a> once to access the PDF.";
}
}
if(isset($message2)){
print "<p>$message2</p>";
}else{
if(isset($message)){
print "<p><b>Your submission has the following problems:</b><br>$message</p>";
}
?>
<form method="post" action="" enctype="multipart/form-data">
<table border="0" cellpadding="0" cellspacing="5" width="200">
<tr>
<td width="300">First name</td>
<td><input name="fname" type="text" maxlength="100" size="25" value="<? if(isset($POST['fname'])) echo $POST['fname']; ?>"></td>
</tr>
<tr>
<td>Last name</td>
<td><input name="lname" type="text" maxlength="100" size="25" value="<? if(isset($POST['lname'])) echo $POST['lname']; ?>"></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" name="submit" value="Submit" id="submit"> <input type="reset" value="Reset"></td>
</tr>
</table>
</form>
<?}?>
I hope someone can help me; it seems like it should be pretty simple, but for the life of me I cannot figure it out.
Thanks,
LW