I have a scenario that has been bugging me for a few days now, so I hope that someone has a solution.
First, here is the scenario I'm trying to code for;
I have documents, (in .PDF format) that I want users to be able to download from my site. After a user downloads a document, I want to (programmatically) gather the number of times a user has downloaded a document, and update that number in a database.
The problem is that my code is saving all the data as session data objects, which then makes the whole "can't send headers after session_start()" issue relevant.
I am using the lastest version of PHP, so using ob_start() before session_start() should work, but it doesn't.
Following is the pseudocode of the php page the I am working on;
<?php
require_once ('/classes/works.php');
if (<$_POST condition>)
{
$fn = 'example.pdf';
$pfn = './downloads/' . $fn;
header("Cache-control: private");
header("Content-type: application/octet-stream\n");
header("Content-disposition: attachment; filename=\"$fn\"\n");
header("Content-transfer-encoding: binary\n");
header("Content-length: " . filesize($pfn) . "\n");
$fp = fopen($pfn, "rb");
fpassthru($fp);
fclose($fp);
session_start(); // starting session after headers.
// get number of downloads of $fn, and update db.
// For some reason the 'work' object does not exist at this point
// Do not understand why:-(
$title2 = $_SESSION['work']->getTitle(); // $title2 is null.
}
else
{
session_start();
if (!isset($_SESSION['work']))
{
$_SESSION['work'] = new Work();
}
$title = $_SESSION['work']->getTitle();
// Display page.
}
?>
Many thanks in advance for any assitance on this.
- Scott