0

I'm attempting to get all HTTP header output from a PHP file, when run via php-cgi. From everything I've read, php-cgi is supposed to output all headers by default. (There's even an option to suppress this, as though it happens automatically.)

I have a PHP file named "test.php", with the following contents:

header('Location: http://stackoverflow.com');
echo 'test';

But when I run it:

php-cgi -f test.php

The output is simply:

test

I expected the location header to be output first. How can I get this header info? I'm using PHP 5.5.3-1ubuntu2.3 (cgi-fcgi).

Sam
  • 7,252
  • 16
  • 46
  • 65
gavanon
  • 1,293
  • 14
  • 15
  • You should put an `exit();` after your `header(...);`. It's logical that "test" is the output of your script. Read this : http://stackoverflow.com/questions/3553698/php-should-i-call-exit-after-calling-location-header – Vincent Decaux Jun 01 '14 at 19:08

2 Answers2

2

I got it! I noticed in the doc definition for the -f argument that it "Implies '-q'".

So this is the solution:

php-cgi test.php

(Without the -f argument)

gavanon
  • 1,293
  • 14
  • 15
-1

The problem is that you are not exiting the script once setting header. You would need to do something more like this

header('Location: http://stackoverflow.com');
exit();

otherwise code after the header() will be run aswell

viralpickaxe
  • 481
  • 4
  • 13
  • Thanks - that won't help either. I figured it out though, but can't answer my own question as a new user. Turns out the problem was the -f argument when calling php-cgi. Docs say "Implies -q", which suppresses headers! – gavanon Jun 01 '14 at 19:21