PHP defines the header by itself

advertisements

I have a script on php, that has some output and I set headers there:

header("Content-Length: 20".$eol);
header("MIME-Version: 1.0".$eol);
header("Connection: Keep-Alive".$eol);
header("Accept-Encoding: gzip, deflate".$eol);
header("Host: host".$eol.$eol);
header("Content-Type: multipart/related; boundary=boundary_.oOo._MzA3NjM=NTczNw==NDM1Ng==".$eol);
echo $res;

I set content-length, but when I check my script the page sets content length as strlen of $res. What's the problem?

EDIT So as I understand code is correct. Problem can be in Webserver. (I'm using Apache)
EDIT $eol="\r\n".
EDIT

$res="--".$BOUNDARY.$eol;
$res .= "Content-Type: application/json".$eol;
$res .= "Content-Length: ".strlen("JSON").$eol.$eol;
$res .= "JSON";
$res.=$eol.$eol;
$res.="--".$BOUNDARY.$eol;
$res.="Content-Type: image/jpeg".$eol;
$res.="Content-Length: ".strlen("Image").$eol;
$res.=$eol;
$res.="Image";
$res.=$eol.$eol;
$res.="--".$BOUNDARY.$eol;
$res .= "Content-Type: application/octet-stream".$eol;
$res .= "Content-Length: ".strlen("Vector").$eol;
$res.=$eol;
$res.="Vector";
$res .= $eol.$eol;
$res .= "--".$BOUNDARY."--".$eol;

Body of response should use MIME. Java coder that checks it asked me to set content-length that is sum of content-length of each entity.


The HTTP protocol follows the robustness principle as described in RFC1122, which states "Be liberal in what you accept, and conservative in what you send". As a result of this principle, HTTP clients will compensate for and recover from incorrect or misconfigured responses, or responses that are uncacheable.

Source

Because you've sent an incorrect Content-Length value, Apache has corrected it for you.