PHP headers not working before a location header

j.edgar Source

On my web application, I want the user to download some information on a page and redirect after the same.

 header('Content-Description: File Transfer');
    header('Content-Type: text/force-donwload');
    header('Content-disposition: attachment; filename=Your_Fibble_Password.txt');
    header('Content-Length: '.strlen($password));
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');
    header('Pragma: public');
    echo $password;

So Here the location header works and the page is been redirected
but the headers for the file transfer above aren't working.
Note: I can't use exit as I got some code executing below it.



answered 4 months ago Jose Mato #1

As far as I know, it's not possible do this. The technical limitation is: "You can't send headers after the content", so, in this case, you are sending the password to download (in txt format file) and then you try to send more headers to indicate a redirection. Maybe you could simulate this behaviour from the frontend application, performing a document.location in javascript.

comments powered by Disqus