views:

34

answers:

3

All,

I have a PHP application that seems to generate the following headers in an array. I wish to remove these headers, so I can set new ones like the one for a pdf download.

array(2) {
  [0]=>
  string(23) "X-Powered-By: PHP/5.3.1"
  [1]=>
  string(23) "Content-type: text/html"
}

How do I do this, as this doesn't seem to work:

var_dump(headers_list());

header_remove("X-Powered-By");
header_remove("Content-type");

var_dump(headers_list());
A: 

You can only modify headers, if no output has been sent to the client yet. Thus var_dump function will cause header_remove to be ignored (since it obviously outputs text to the client).

You should also take a look out output buffering functions: http://us3.php.net/outcontrol

sukru
A: 

in a cli application you have to add a -q parameter to you php-cli.

#!/usr/bin/php -q

but in a web application it's a little complex; (I can't test it now)

ob_start(); // turn on output buffer
ob_implicit_flush(0); //
print "\n"; // print something so the header goes out
$headerData = ob_get_contents(); // your headers will be in this variable
ob_end_clean();

also you can remove trough php.ini

expose_php = Off
Ehsan
+2  A: 

I think the lines may do the same what you exactly want to do.

header("Content-type: application/pdf");
header('Content-Disposition: attachment; filename="sample.pdf"');
readfile('/var/www/sample.pdf');

Thanks

Muhit
This seems to work, but instead of asking the user to save the file, it prints the pdf contents in the browser. How to avoid this?
Vincent