4

I've been playing with output_buffering in php (confirmed by phpinfo()) and I just discovered that upon turning it on, I will start getting random 404 errors on my pages, but the page content loads fine and everything looks ok.

This only happens on my production site. PHP 5.5.35, Apache/2.4.18 (Unix). I have no idea where this 404 is being sent from. Does anyone have any ideas as to what might be happening, or what I can search for on my server to fix it?

These are my response headers in case that's at all relevant:

Cache-Control:s-maxage=10
Cache-Control:no-cache, must-revalidate, max-age=0
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:13021
Content-Type:text/html; charset=UTF-8
Date:Fri, 02 Feb 2018 18:31:59 GMT
Expires:Wed, 11 Jan 1984 05:00:00 GMT
Keep-Alive:timeout=5, max=96
Server:Apache
Strict-Transport-Security:max-age=31536000; includeSubDomains; preload
Vary:Accept-Encoding
X-Powered-By:PHP/5.5.35

EDIT: I discovered that this issue only happens on pages on which I am loading 'recent posts' from a Wordpress instance. It's not really an option for me to remove this little widget but I assume there's a reason behind setting a 404 header. Any thoughts?

mcheah
  • 1,209
  • 11
  • 28
  • It is technically OK to have a page with response code = 404 and have (potentially useful) content (i) could you tell us what is the expected resut...404 or 200 (ii) Could you confirm if the behavior is random or predictable? – Salman A Feb 06 '18 at 17:28
  • I expect a 200, and the behavior is..odd. I will sometimes see pages with with a 404 error and hard refreshing does nothing - the error comes back. Then I'll come in the next morning and every page that was not working will come back with a 200 status. So I guess unpredictable, but not from one second to the next. – mcheah Feb 06 '18 at 17:38
  • Strange. Anyway, check PHP error logs for "cannot modify header" or similar. Post any errors that you find. – Salman A Feb 06 '18 at 18:26

1 Answers1

1

I think what is happening is that your code is issuing 404s in some cases regardless of whether output buffering is on or off; but when output buffering is off the header fails to be set because it has already been written generating a warning like this one:

Warning: Cannot modify header information - headers already sent by (output started at /file.php:20) in /file.php on line 100

So you can only see the 404 when output buffering is on which enables you to set headers at any point before the buffers are written to output.

dhinchliff
  • 1,106
  • 8
  • 17
  • This is possible, but the only times I receive that error is when my recent posts Wordpress widget attempts to set cache-control headers. I can't imagine why Wordpress would send a 404, and from what point in the code it would originate from. Do you have any recommendations for finding out? – mcheah Feb 06 '18 at 17:42
  • Set an error handler to catch warnings and see if this is the case, if there is a warning like the one i'm suggesting you message will have file name and line number. – dhinchliff Feb 06 '18 at 17:44
  • Maybe you can send all warning to a log file https://codex.wordpress.org/Debugging_in_WordPress – dhinchliff Feb 06 '18 at 17:46
  • I've definitely gotten the "cannot modify header information" error continually, but it refers back to the wordpress functions.php file for setting nocache headers. I think you're right though, something here is sending a 404 and it's more of a Wordpress question than an output_buffering question. – mcheah Feb 06 '18 at 22:44