Jump to content

calling a function after sending header information


validkeys

Recommended Posts

On my site, users can only download the same file once a month. I use direct link masking so that the user cant see the actual direct link of the file. When the user clicks on the download link next to the file, i run the following:

 

$full_name = $realDLfolder.$path.$filename;
//echo $full_name;
$xtype="audio/mpeg";

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: $xtype");
header("Content-Disposition: attachment; filename=\"".basename($full_name)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($full_name));






@readfile($full_name);
exit;

 

After this program runs, however, I need to call a javascript function that then adds this file to the database so that I know that the user has downloaded this file. How can I call this function after the above code? If I call it before the headers are sent I will get an error so it has to be after this program above

 

any help would be amazing

 

thanks so much

Why are you calling a javascript function, why not just include that function into this script to run "silently"?

 

IE:

$full_name = $realDLfolder.$path.$filename;
//echo $full_name;
$xtype="audio/mpeg";

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: $xtype");
header("Content-Disposition: attachment; filename=\"".basename($full_name)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($full_name));

//update database here:
mysql_query("UPDATE users SET downloaded = 'y', date = '".time()."' WHERE username = '$uname'");

//OR:
include('updateDownloadRecord.php');

@readfile($full_name);
exit;

 

Why use javascript when you can just use php and have everything hidden?

I see, well than what you want is on the html page, after the link is clicked to update the AJAX.

 

Because this page is called separate of your html page and trying to invoke a JS function won't work because it does not have that JSCode.

 

When the user clicks on the link, send something to Ajax saying, "Remove this item after x seconds".

well i tried what you said and the problem is I think that the download.php script moves faster than the ajax and the request doesnt have time to go all the way through. I tried to slow it down by placing an alert message in my ajax function that handles the responsetext. If i have a alert message on each state change the javascript function will run find and update my database like I had mentioned. If I dont place this alert message in the responsehandler function it doesnt update

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.