validkeys Posted March 21, 2007 Share Posted March 21, 2007 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 Link to comment https://forums.phpfreaks.com/topic/43679-calling-a-function-after-sending-header-information/ Share on other sites More sharing options...
per1os Posted March 21, 2007 Share Posted March 21, 2007 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? Link to comment https://forums.phpfreaks.com/topic/43679-calling-a-function-after-sending-header-information/#findComment-212044 Share on other sites More sharing options...
validkeys Posted March 21, 2007 Author Share Posted March 21, 2007 well because I am using ajax and after the user downloads that file, I refresh the file downloads section of the page that they are on and take away the download link Link to comment https://forums.phpfreaks.com/topic/43679-calling-a-function-after-sending-header-information/#findComment-212047 Share on other sites More sharing options...
per1os Posted March 21, 2007 Share Posted March 21, 2007 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". Link to comment https://forums.phpfreaks.com/topic/43679-calling-a-function-after-sending-header-information/#findComment-212051 Share on other sites More sharing options...
validkeys Posted March 21, 2007 Author Share Posted March 21, 2007 how would i send that message to ajax from this link? I already have an <A HREF> for the link. Link to comment https://forums.phpfreaks.com/topic/43679-calling-a-function-after-sending-header-information/#findComment-212104 Share on other sites More sharing options...
per1os Posted March 21, 2007 Share Posted March 21, 2007 <A href="yourscript.php?download=1" onClick="ajaxUpdate(this);">Click here!</a> Now the ajaxUpdate and this need to be done correctly but that is the basic gist. Link to comment https://forums.phpfreaks.com/topic/43679-calling-a-function-after-sending-header-information/#findComment-212113 Share on other sites More sharing options...
validkeys Posted March 21, 2007 Author Share Posted March 21, 2007 okay cool. thats what i thought. I wasnt sure you could do the onClick in an a href. Stupid me. Thanks so much for your help. I really appreciate it! Have a great day Link to comment https://forums.phpfreaks.com/topic/43679-calling-a-function-after-sending-header-information/#findComment-212114 Share on other sites More sharing options...
validkeys Posted March 22, 2007 Author Share Posted March 22, 2007 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 Link to comment https://forums.phpfreaks.com/topic/43679-calling-a-function-after-sending-header-information/#findComment-212483 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.