Problem with PDF download script and database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lwebb
Forum Newbie
Posts: 3
Joined: Tue May 09, 2006 6:20 pm

Problem with PDF download script and database

Post 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;
}
?>
lwebb
Forum Newbie
Posts: 3
Joined: Tue May 09, 2006 6:20 pm

maybe it is fpassthru?

Post 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.
???
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Call session_write_close before you output the headers and content... Might help.
lwebb
Forum Newbie
Posts: 3
Joined: Tue May 09, 2006 6:20 pm

more info

Post 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.
Post Reply