-1

I have problem with my web server hosted in php:8.0-apache docker container, always returning status code 200, even if it's set to different value. Same code outside of container works correctly

my php.ini file is:

log_errors=On
error_log=/dev/stderr

and php code is:

... some code ...

http_response_code(400);
exit;

but I receive response:

HTTP/1.1 200 OK

Any ideas what's wrong with my setup?

edit: docker file:

FROM php:8.0-apache

RUN echo "<?php echo '{some json content}'; http_response_code(400); exit; ?>" >> /var/www/html/index.php

COPY php.ini /usr/local/etc/php/
Yanny
  • 178
  • 12

1 Answers1

0

I figured what was the problem:

there was already sent header with status 200 when I called echo "...", so http_response_code(400) was ignored.

To solve this issue (and reason why it was working with default configuration on my pc) is to set output_buffering in php.ini to some value, so output is not sent immediately.

log_errors=On
error_log=/dev/stderr
output_buffering=4096

see: https://stackoverflow.com/a/8028987/1557025

Yanny
  • 178
  • 12
  • Yeah, it's a solution but if you can move the logic to change response code to before the very first echo, you will be able to turn the output buffering off which will send the response to your client faster and reduce memory footprint in your server. – Truong Hua Nov 04 '21 at 16:32