Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the HTTP response code not available in the PHP $_SERVER variable?

Why is the HTTP response code (or the first line of the HTTP response header) not available in the PHP $_SERVER variable array?

Is it a limitation on the way server technology works? A limitation with PHP? With the HTTP protocol? Or maybe I'd be completely NUTS to ever want the HTTP response code? ;)

In my case, I'm working with Apache2.

Just curious.

EDIT 1: I'm inquiring about the equivalent of %>s (the status of the LAST request) used by Apache's LogFormat directive before the server response is sent.

EDIT 2: The answers make perfect sense, but the question remains, why can't I get my server's internal working response code? If I set an ErrorDocument 403 /index.php, I can access the response code with $_SERVER['REDIRECT_STATUS']. Isn't there an easier way? I'm guessing not. I still learned a lot from your answers, however. It makes sense.

There's also apache_response_headers(), but still no love for the working response code/first line.

like image 520
Jeff Avatar asked Mar 07 '26 06:03

Jeff


1 Answers

Because the HTTP response is something that is sent to the client, and you can change it within your PHP using the header function. It's not something that is automatic within your PHP and would be handled by the server (ie apache) before it would be given over to PHP.

like image 131
Jon Avatar answered Mar 08 '26 20:03

Jon