简体   繁体   中英

content-type wasn send by setting it with header() in php

I have a problem with setting the content-type by the php function header(); My complete code doesn't ever send any output. Only the response-object does it. Before every output i send the headers. Depending on the Accept-Header i set a Content-Type and do the right output, it in an easy way it looks like that:

switch($aceptHeader){ 
  case 'text/html': 
  #and some othe stupid acept-headers of the ie
    $this->setHeader('Content-Type: text/html; charset=".."');
    $this->sendHeaders();
    $this->sendDefault();
    break;
  case 'application/json':
    $this->setHeader('Content-Type: application/json');
    $this->sendJSON();
    break;  
  case 'application/xml':
    $this->setHeader('Content-Type: application/xml');
    $this->sendXML();
    break;
}

the method for the setHeader fills only an array, wicht will be put by an foreach-loop into the header(); function. This is not important, cause i tried it in the direct way by changing it $this->setHeader() to header('Content-Type..'); it has got the same result (later) The output ist just an rendering of an array by an twig template. This is the only output. The setting of the headers is not the problem. I even get a positive result, when i do a print_r(header_list()); . There i see exactly that header i set before. I see the same response header in almost every browser, but not in every: the ie8 shows me only the html-code as a string in the body after a pre-tag. This was the reason, why i tested my page by the w3c validator (and other tools). All showed the same result:

they get no Content-Type header

what could be my mistake? Could there be any mistake before i set the header? - id do no wild inlcude stuff or anything else - the setting of the header make no trouble - the only headers i change in the models between request and response is the http status, wich is followed by an $response-send(); in almost every time.

Could there be a manipulation of the header while rendering the twig-template?

The Question is: What could disturb the setting of the content-type header by the header() function?

An interesting thing ist: i put following code to the begining of my index.php

header('Content-Type: text/html');
echo 'test';
exit;

and than i get green test by all validation tool. The header was send.

I could go throught my whole code and look where i loose the header, but this could be a long long way. Is there anybody that has had the same problem or could imagine what i have done wrong?

This is only an look-a-like example. The switch block is not the problem, the sendHeader not, too. Cause i replaced them in different cases the whole afternoon by setting the header directly - every time with the same result.

I used these output switch for different cases and it works fine every time. I send json, with an header that $.ajax() of jQuery accepts as an json and not as an string. I produced xml for my RESTful api, that works fine and sends the data in that format i wanted it. Only the validator tool and the ie8 doesn't like it. And I don't understand why...

switch($aceptHeader){ 
  case 'text/html': 
  #and some othe stupid acept-headers of the ie
    $this->setHeader('Content-Type: text/html; charset=".."');
    $this->sendHeaders();
    $this->sendDefault();
    break;
  case 'application/json':
    $this->setHeader('Content-Type: application/json');
    $this->sendJSON();
    break;
  case 'application/xml':
    $this->setHeader('Content-Type: application/xml');
    $this->sendXML();
    break;
}

Forgot the breaks in which tell the code when to try the next case.

I solved the Problem.

And...

it was a problem of the implementation. It was a thing you can't see, cause i don't post it. Indeed it was the default path, where i send that wath the client wants to get as Content-Type. Mean if the client (IE8 or validation tools) sends no accept-header or an empty string i send and "Content-Type: " this is fail. I could cry. grrrrrrrrrr. Sorry for that.

To let you know my right implementation (needs some refactoring i know):

    $acceptFormat = $this->request->getAcceptFormat();
#do a switch for the type of the output and send the headers before
switch ($acceptFormat ){
  case 'text/html':
  case 'application/x-www-form-urlencoded':
  case 'application/x-ms-application':
  case 'application/vnd.wap.xhtml+xml':
  case '*/*':
    $this->setHeader('content-type: text/html; charset=UTF-8');
    $this->_sendHeaders();
    $this->_sendDefault();
    break;
  case 'application/json':
    $this->setHeader('Content-Type: application/json');
    $this->_sendHeaders();
    $this->_sendJSON();
    break;
  case 'application/xml':
    $this->setHeader('Content-Type: application/xml');
    $this->_sendHeaders();
    $this->_sendXML();
    break;
  default:
    $this->debugger->log('Unknown Accept-Header set: '.$acceptFormat.' for setting the Content-Type ');
    #$this->setHeader('content-type: '.$acceptFormat);
    $this->setHeader('content-type: text/html; charset=UTF-8');
    $this->_sendHeaders();
    $this->_sendDefault();
    break;
} 

the commented part was the fault.

oh my god. ...

but i could never imagine, that a browser would ever send nothing as accept-type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM