Jump to content

Recommended Posts

Hi All. I want to introduce logging for downloads on our site. To achieve this, each downloadable file will include a small form with a download button. The file path is saved in a hidden form value.

 

Each form posts to download.php, which brings the file name to a disabled input field, and prompts the user for their details before continuing to the download. On completing their details, this posts to downloader.php, which logs the download and users details to a mysql database, presents the user with a "if your file didn't automatically download click here" message, and uses headers to attempt to force the download.

 

Problem A is that the logging doesn't work. I do have one record in the downloadlog table, but on trying again no new records are created..

 

Problem B is that the automatic download using headers does not work. If I place the php header code above the html code, it is executed without displaying the page, and the incorrect filename is given. Clicking on my manual download link works absolutely fine and holds the same path and filename as given to the automatic download. If I place the php header code after my page content, it does not run at all.

 

 

Page 1 - product page form:-

 

<form id="form1" method="post" action="../download.php">
            <input name="file" type="hidden" id="file" value="../pdf/2009/Introduction_to_Sage_200_Brochure.pdf" />
            <input style="border: 1px groove rgb(0, 0, 0); margin-bottom:5px; margin-top:5px; background:transparent url('../img/button_bg1.jpg'); text-align: center; font-family: tahoma; font-size: 12px; color: rgb(255, 255, 255); font-weight:bold" name="download" type="submit" class="pincodebox" id="download" value="Download" /></form>

 

Posts to page 2: download.php:

<?php
putenv("TZ=Europe/London");
header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
session_cache_expire(1440);
session_start();
if(!empty($_POST['file'])) $_SESSION['file'] = htmlspecialchars($_POST['file']);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Century Software - Sage 200 Bill of Materials (BOM)</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="cache-control" content="no-store, no-cache, max-age=0, must-revalidate">
<meta http-equiv="pragma" content="no-cache">
<link rel="stylesheet" href="style.css" type="text/css" media="screen,projection" />
<script type="text/javascript">
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value==""||value=="xx"||value=="123")
  {alert(alerttxt);return false;}
else {return true}
}
}
function validate_form(form1){
with (form1){

if (validate_required(name,"Please enter your name to continue")==false)
  {name.focus();return false;}
if (validate_required(company,"Please enter your company name to continue")==false)
  {company.focus();return false;}
if (validate_required(email,"Please enter your valid email address to continue")==false)
  {email.focus();return false;}
}
}
</script>
<style type="text/css">
<!--
.style1 {font-size: 1.2em}
.style5 {font-size: 18px}
.style8 {font-size: 8px}
.style15 {font-size: 14px; font-weight: bold; font-style: italic; color: #036A67; }
.style19 {font-size: 12px; font-weight: bold; font-style: italic; color: #036A67; }
.style27 {font-size: 10px; }
.style28 {font-size: 1.2em; font-weight: bold; }
.style29 {font-size: 14px}
.style30 {font-size: 10px; font-weight: bold; }
-->
</style>
</head>

<body><div id="container" >

<div id="header">
<h1>
  <iframe src="../fd_header.php" width="758" height="101" scrolling="no" frameborder="0"></iframe>
  </h1>
</div>

<div id="navigation">
<ul>
<li><a href="index.html" title="Home Page">Home</a></li>
<li><a href="aboutus.html" title="About Us">About Us</a></li>
<li class="selected"><a href="products/index.html" title="Products">Products</a></li>
<li><a href="services/index.html" title="Services">Services</a></li>
<li><a href="news.html" title="Company and Client News">News</a></li>
<li><a href="partners.html" title="Our Partners">Partners</a></li>
<li><a href="events.html" title="Events">Events</a></li>
<li><a href="case-studies.html" title="Case Studies">Case Studies</a></li>
<li><a href="contactus.html" title="Contact Us">Contact Us</a></li>
<li><a href="support/index.html" title="Support">Support</a></li>
</ul>
</div>

<div id="content">

<div class="splitcontentleft">
  <h2>Products » Downloads</h2>
  <div align="justify">
    <div class="box">
      <h3>Download File</h3>
      <p><span class="style1">Please enter brief details below in order to continue to your download.</span><br />
      </p>
      <form id="form1" method="post" action="downloader.php" onsubmit="return validate_form(this);">
        <div align="center">
          <table width="98%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td height="26" align="right" class="style28">Requested File: </td>
              <td align="left" class="style5"><span class="style6 style5">
                <input name="file" type="text" disabled="disabled" class="style11" id="file" style="border: 1px groove #036B6A; font-family:tahoma; font-size:14px; background:transparent url('img/field_bg4.gif')" value="<?php echo $_SESSION['file']; ?>" size="40" maxlength="200"/>
              </span></td>
            </tr>
            <tr>
              <td height="26" align="right" class="style28">Your Name: </td>
              <td align="left" class="style5"><span class="style6 style5">
                <input name="name" type="text" class="style11" id="name" style="border: 1px groove #036B6A; font-family:tahoma; font-size:14px; background:transparent url('img/field_bg4.gif')" size="40" maxlength="50"/>
              </span></td>
              
            </tr>
            <tr>
              <td height="26" align="right" class="style28">Company Name: </td>
              <td align="left" class="style5"><span class="style6 style5">
                <input name="company" type="text" class="style11" id="company" style="border: 1px groove #036B6A; font-family:tahoma; font-size:14px; background:transparent url('img/field_bg4.gif')" size="40" maxlength="100"/>
              </span></td>
            </tr>
            <tr>
              <td height="26" align="right" class="style28">Email Address: </td>
              <td align="left" class="style5"><span class="style6 style5">
                <input name="email" type="text" class="style11" id="email" style="border: 1px groove #036B6A; font-family:tahoma; font-size:14px; background:transparent url('img/field_bg4.gif')" value="" size="40" maxlength="100"/>
              </span></td>
            </tr>
          </table>
          <p></p>
          <p></p>
        </div>
        <p align="center">
          <input name="submit" type="submit" class="style11" id="submit" value="Continue To Download..." style="border: 1px groove rgb(0, 0, 0); background:transparent url('img/button_bg1.jpg'); text-align: center; font-family: tahoma; font-size: 14px; color: rgb(255, 255, 255); font-weight:bold; width:200px" onclick="submitPressed()" /></p>
</form>
    <p> </p>
    </div>
  </div>
  </div>
</div>

<div id="subcontent">
   <p align="center">
     <iframe src="../goback2.htm" width="162" height="32" scrolling="no" frameborder="0"></iframe>
   </p>
</div>
<div id="psubcontent">
   <p align="center">
     <iframe src="../fd_logos.htm" width="162" height="162" scrolling="No" frameborder="0"></iframe>
   </p>
  </div>
<div id="footer">
<p>©2009 <a href="aboutus.html">Century Computing Software Services Ltd</a></p>
<p align="center" class="small small style5"><span class="style8" lang="EN-GB" xml:lang="EN-GB">Registered in   England No: 4374825.  VAT Registration No GB 792 5317 09. 
  With registered   offices at Century Computing Software Services Ltd<br /> 
  c/o GCN Accounting Services   Ltd, 
  Dominique House, 1 Church Road, Netherton, Dudley, West Midlands DY2   0LY.</span></p>
</div>
</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10217342-1");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>

 

Posts to page 3: downloader.php

<?php
putenv("TZ=Europe/London");
header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
session_cache_expire(1440);
session_start();
if(!empty($_POST['file'])) $_SESSION['file'] = ($_POST['file']);
if(!empty($_POST['name'])) $_SESSION['name'] = htmlspecialchars($_POST['name']);
if(!empty($_POST['company'])) $_SESSION['company'] = htmlspecialchars($_POST['company']);
if(!empty($_POST['email'])) $_SESSION['email'] = htmlspecialchars($_POST['email']);

$file = $_SESSION['file'];
$name = $_SESSION['name'];
$company = $_SESSION['company'];
$email = $_SESSION['email'];

$dbhost = 'localhost';
$dbuser = '**************';
$dbpass = '**************';
$dbname = 'downloads';
$table = 'downloadlog';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to database');

mysql_select_db($dbname);

@mysql_select_db("$dbname") or die("Error: Unable to select database");

putenv("TZ=Europe/London");

$datetime = date("Y-m-d H:i:s");

$sqlquery = "INSERT INTO $table
VALUES('','$datetime','$name','$company','$email','$file','','','','','')";

mysql_query($sqlquery);

mysql_close($conn);

if(!file)
{
     // File doesn't exist, output error
     die("Unable to locate requested download file. Please go back and try again.");
}
else
{
     // Set headers
     header("Cache-Control: public");
     header("Content-Description: File Transfer");
     header("Content-Disposition: attachment; filename=$file");
     header("Content-Type: application/pdf");
     header("Content-Transfer-Encoding: binary");
    
     // Read the file from disk
     readfile($file);
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Century Software - Downloads</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="cache-control" content="no-store, no-cache, max-age=0, must-revalidate">
<meta http-equiv="pragma" content="no-cache">
<link rel="stylesheet" href="style.css" type="text/css" media="screen,projection" />
<style type="text/css">
<!--
.style1 {font-size: 1.2em}
.style5 {font-size: 18px}
.style8 {font-size: 8px}
.style15 {font-size: 14px; font-weight: bold; font-style: italic; color: #036A67; }
.style19 {font-size: 12px; font-weight: bold; font-style: italic; color: #036A67; }
.style27 {font-size: 10px; }
.style28 {font-size: 1.2em; font-weight: bold; }
.style29 {font-size: 14px}
.style30 {font-size: 10px; font-weight: bold; }
-->
</style>
</head>

<body><div id="container" >

<div id="header">
<h1>
  <iframe src="../fd_header.php" width="758" height="101" scrolling="no" frameborder="0"></iframe>
  </h1>
</div>

<div id="navigation">
<ul>
<li><a href="index.html" title="Home Page">Home</a></li>
<li><a href="aboutus.html" title="About Us">About Us</a></li>
<li class="selected"><a href="products/" title="Products">Products</a></li>
<li><a href="services/" title="Services">Services</a></li>
<li><a href="news.html" title="Company and Client News">News</a></li>
<li><a href="partners.html" title="Our Partners">Partners</a></li>
<li><a href="events.html" title="Events">Events</a></li>
<li><a href="case-studies.html" title="Case Studies">Case Studies</a></li>
<li><a href="contactus.html" title="Contact Us">Contact Us</a></li>
<li><a href="support/" title="Support">Support</a></li>
</ul>
</div>

<div id="content">

<div class="splitcontentleft">
  <h2>Products » Downloads</h2>
  <div align="justify">
    <div class="box">
      <h3>Download</h3>
      <p><span class="style1">Thank you <strong><?php echo $_SESSION['name']; ?></strong>.</span></p>
      <p><span class="style1"><em>Your download should start automatically within the next few seconds. If it does not please <a href="<?php echo $file ?>" target="_blank"><strong>click here</strong></a> to download manually.</em></span></p>
      <p class="style1"><strong><font color="#CC0000"><?php echo $errortxt ?></font></strong></p>
      <p class="style1"><a href="javascript:history.go(-2)" title="Go Back..">Click here to return to the page you were previously viewing.</a></p>
    </div>
  </div>
  </div>
</div>
<div id="subcontent">
   <p align="center">
     <iframe src="../goback.htm" width="162" height="32" scrolling="no" frameborder="0"></iframe>
   </p>
</div>
<div id="psubcontent">
   <p align="center">
     <iframe src="../fd_logos.htm" width="162" height="162" scrolling="No" frameborder="0"></iframe>
   </p>
  </div>
<div id="footer">
<p>©2009 <a href="aboutus.html">Century Computing Software Services Ltd</a></p>
<p align="center" class="small small style5"><span class="style8" lang="EN-GB" xml:lang="EN-GB">Registered in   England No: 4374825.  VAT Registration No GB 792 5317 09. 
  With registered   offices at Century Computing Software Services Ltd<br /> 
  c/o GCN Accounting Services   Ltd, 
  Dominique House, 1 Church Road, Netherton, Dudley, West Midlands DY2   0LY.</span></p>
</div>
</div>
</body>
</html>

 

If want want to try this yourself, and please do to show you what I mean, then start at this page: http://www.centurysoftware.co.uk/products/sage-200-bill-of-materials.html scroll down until you see the Sage 200 Brochure and download button for this.

 

Try the manual link and look at the path. The automatic download tries to use an incorrect filename, but it's using the same variable. Now I can't get the automatic download to work at all.

 

Any help would be greatly appreciated. Thanks for your time.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.