PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 27, 2022

[FIXED] CakePHP 3 - Warning shown due to length of content?

 February 27, 2022     cakephp, cakephp-3.0     No comments   

Issue

I've got a strange problem whereby I'm seeing the following PHP Warnings:

Warning (512): Unable to emit headers. Headers sent in file=/vendor/cakephp/cakephp/src/Error/Debugger.php line=921 [CORE/src/Http/ResponseEmitter.php, line 48]
Code Context
Cake\Http\ResponseEmitter::emit() - CORE/src/Http/ResponseEmitter.php, line 48
Cake\Http\Server::emit() - CORE/src/Http/Server.php, line 105
[main] - ROOT/webroot/index.php, line 37
Warning (2): Cannot modify header information - headers already sent by (output started at /vendor/cakephp/cakephp/src/Error/Debugger.php:921) [CORE/src/Http/ResponseEmitter.php, line 148]
Code Context
header - [internal], line ??
Cake\Http\ResponseEmitter::emitStatusLine() - CORE/src/Http/ResponseEmitter.php, line 148
Cake\Http\ResponseEmitter::emit() - CORE/src/Http/ResponseEmitter.php, line 54
Cake\Http\Server::emit() - CORE/src/Http/Server.php, line 105
[main] - ROOT/webroot/index.php, line 37
Warning (2): Cannot modify header information - headers already sent by (output started at /vendor/cakephp/cakephp/src/Error/Debugger.php:921) [CORE/src/Http/ResponseEmitter.php, line 181]
Code Context
header - [internal], line ??
Cake\Http\ResponseEmitter::emitHeaders() - CORE/src/Http/ResponseEmitter.php, line 181
Cake\Http\ResponseEmitter::emit() - CORE/src/Http/ResponseEmitter.php, line 55
Cake\Http\Server::emit() - CORE/src/Http/Server.php, line 105
[main] - ROOT/webroot/index.php, line 37

I'm in development mode ('DEBUG', true in app/config.php). If I set that to false the Warnings disappear, as expected.

The error is originating from this Controller method:

public function ajaxSubstances()
{
    $this->autoRender = false;
    $SubstanceModel = new SubstanceModel;
    $data = ['data' => $SubstanceModel->getSubstances()];
    debug($data);
}

What I don't understand is that if my $data array only contains, say 20 elements, no Warning is shown. As I gradually add more elements it always shows the Warning once there is a certain number of records (approx >=30 elements).

To check it's not the source data (from getSubstances()) I hardcoded a $data array in, but the same thing happens.

This method will be used as an ajax response and be fed into DataTables. Unfortunately if the Warnings are output, DataTables will show an error because it's not a valid response.

I can't switch debug to false because I need to have that on for the rest of my development. And I'd like to know why this may be happening anyway?

I'm not sure why it's trying to "emit headers" because the header type isn't being changed anywhere in the code - it's outputting everything using debug(), and I also tried var_dump() but the output is still the same text/html content type.


Solution

It's not necessarily due to the length of the content, but generally due to outputting data before the emitter can output headers, it will happen once there's even a single byte being sent before the response emitter comes into play, which explicitly tests for headers_sent().

Note that there are further headers that may need to be emitted besides those that you might have applied to the response yourself, like cookies, content length, etc.

You most probably only see the warning as of a certain amount of data because you're using PHPs output buffering and/or compression (see output_buffering and zlib.output_compression in your php.ini), which will only automatically flush data in the midst of the script in case the buffer storage capabilities are being exceeded (in most cases that's usually 4096 bytes).

See also

  • http://php.net/manual/en/book.outcontrol.php
  • http://php.net/manual/en/book.zlib.php


Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing