Page 1 of 1

Problem with PDF download script and database

Posted: Tue May 09, 2006 6:29 pm
by lwebb
This script works fine, except it does 2 inserts for every 1 PDF download. I can't figure out why. Any help would be greatly aprreciated.

Code: Select all

<?php 

session_start(); 
header("Cache-control: private"); 

$_SESSION['validuser'] = "validuser";//for testing


if(isset($_SESSION['validuser'])){

	$hostname = xxx;
	$username = xxx;
	$password = xxx;
	$database = xxx;

	$mysql_link = mysql_pconnect($hostname,$username,$password) or die (mysql_error());
	mysql_select_db($database, $mysql_link) or die (mysql_error());

	$q2 = "insert into pdflog (user_id, pdf_id, access_date) values ('30','3', NOW())";
	$res2 = mysql_query($q2, $mysql_link);

	$path = '/www/pdfs/'."test.pdf";

	header('Content-Type: application/pdf'); 

	$fp = fopen($path, 'rb'); 
	fpassthru($fp); 

}else{
echo "error";
exit;
}
?>

maybe it is fpassthru?

Posted: Wed May 10, 2006 6:59 am
by lwebb
when I substitute the fpassthru with an echo, it only updates once, but when I put the fpassthru back in it updates twice.
???

Posted: Wed May 10, 2006 9:50 am
by timvw
Call session_write_close before you output the headers and content... Might help.

more info

Posted: Wed May 10, 2006 8:04 pm
by lwebb
I figured out that the problem lies in this line of code:

Code: Select all

header("Cache-control: private"); // IE Fix.
When accessed using Netscape, the script only does one insert. But in IE, it does two. If I remove the line of code above, the PDF won't download in IE, instead I get the much discussed IE/php download errors.