Jack Smit
Jack Smit

Reputation: 3113

Show PHP header in FireBug

I have index.php running on WAMP and all I want to do is return 200 Ok header. I have:

echo "Index3<br />";
//All I need to do is execute a method.
$request_method = strtolower($_SERVER['REQUEST_METHOD']);
echo "Serving " .$request_method;
//And send back a response header.
header('HTTP/1.1 200 Ok');
foreach (getallheaders() as $name => $value) {
    echo "$name: $value\n<br />";
}

I see nothing in Firebug(in both the Console and Net tab). In the foreach I see all headers about Accept, Connect, Cache etc. but no 200? I only get something in the Console when I change to a 400 response in the code above, why?

Upvotes: 0

Views: 421

Answers (2)

jimbo
jimbo

Reputation: 11042

Your call to header() needs to come before any echo statements. Once you start echoing content, it's too late to modify the HTTP headers.

This is because the headers precede the HTTP body. If you can't control the use of echo before header calls, you can use output buffers---ob_start() and ob_end_flush()---to prevent them from interfering with the headers.

Upvotes: 0

Mitch Dempsey
Mitch Dempsey

Reputation: 39869

The headers must be the first thing sent to the browser. You have echo statements before your header. You cannot do this.

Also, the HTTP/1.1 200 OK is not considered a "header" like Accept, Cache are. It will generally be available under some statusCode property. Firebug should show it in the "Network" tab. It will be listed next to each request.

Upvotes: 1

Related Questions