Hi,
header("Content-type: text/plain") or header("Content-type: text/xml")
does not work as soon as a default_charset is enabled in php.ini.
I reported that in <http://bugs.php.net/bug.php?id=19098> a week ago,
but nobody answered yet. This bug is still there in php-4.2.3RC2.
If you use the mod_php version with Apache, you will get "Content-type:
text/html" if you use header("Content-type: text/plain") or
header("Content-type: text/xml").
Here's a small test script (header_test.php) to show the problem. It
calls the CGI version of php (in the current directory) and tries a few
Content-type headers:
<?
$PHP = "./php -c .";
$Headers = array("Content-type: text/plain",
"Content-type: text/xml",
"Content-type: application/xml",
"Content-type:text/plain",
"Content-type:text/plain ",
"Content-type:text/longertextsubtype",
"Content-type: text/plain;charset=iso-8859-1",
"Content-type: text/plain; charset=UTF-8");
foreach($Headers as $Header)
{
$Cmd = "echo \"<?php header('$Header') ?>\" | $PHP ";
unset($Output);
exec($Cmd, $Output);
print "Header: \"$Header\"\nOutput: \"$Output[1]\"\n\n";
}
?>
------------------------------------------------------------------------
The output of that script:
# ./php -c . -q header_test.php
Header: "Content-type: text/plain"
Output: "Content-type"
Header: "Content-type: text/xml"
Output: "Content-type"
Header: "Content-type: application/xml"
Output: "Content-type: application/xml"
Header: "Content-type:text/plain"
Output: "Content-type"
Header: "Content-type:text/plain "
Output: "Content-type"
Header: "Content-type:text/longertextsubtype"
Output: "Content-type"
Header: "Content-type: text/plain;charset=iso-8859-1"
Output: "Content-type: text/plain;charset=iso-8859-1"
Header: "Content-type: text/plain; charset=UTF-8"
Output: "Content-type: text/plain; charset=UTF-8"
So if you don't explicitly set the charset, like in the last two tests,
the output is wrong.
------------------------------------------------------------------------
A small patch that makes this right in most cases:
# diff -u php-4.2.3RC2/main/SAPI.c.orig php-4.2.3RC2/main/SAPI.c
--- php-4.2.3RC2/main/SAPI.c.orig Sat Jul 27 15:15:42 2002
+++ php-4.2.3RC2/main/SAPI.c Wed Sep 4 19:07:50 2002
@@ -261,11 +261,12 @@
newtype = emalloc(newlen + 1);
PHP_STRLCPY(newtype, *mimetype, newlen + 1, len);
strlcat(newtype, ";charset=", newlen + 1);
+ strlcat(newtype, charset, newlen + 1);
if (*mimetype != NULL) {
efree(*mimetype);
}
*mimetype = newtype;
- return newlen;
+ return newlen - 1;
}
return 0;
}
@@ -461,7 +462,7 @@
sapi_header.header = newheader;
sapi_header.header_len = newlen - 1;
colon_offset = strchr(newheader, ':');
- *colon_offset = '\0';
+ /* *colon_offset = '\0'; */
efree(header_line);
}
------------------------------------------------------------------------
The test script output after this patch has been applied:
# ./php -c . -q header_test.php
Header: "Content-type: text/plain"
Output: "Content-type: text/plain;charset=iso-8859-1"
Header: "Content-type: text/xml"
Output: "Content-type: text/xml;charset=iso-8859-1"
Header: "Content-type: application/xml"
Output: "Content-type: application/xml"
Header: "Content-type:text/plain"
Output: "Content-type: text/plain;charset=iso-8859-"
Header: "Content-type:text/plain "
Output: "Content-type: text/plain;charset=iso-8859-"
Header: "Content-type:text/longertextsubtype"
Output: "Content-type: text/longertextsubtype;charset=iso-8859-"
Header: "Content-type: text/plain;charset=iso-8859-1"
Output: "Content-type: text/plain;charset=iso-8859-1"
Header: "Content-type: text/plain; charset=UTF-8"
Output: "Content-type: text/plain; charset=UTF-8"
Only the cases without a space after the colon are still garbled.
RFC 2616, section 4.2, says: "The field value MAY be preceded by any
amount of LWS, though a single SP is preferred."
So people should be able to use header("Content-type:text/plain") as
well, but I can't find the remaining bug.
Any comments, please?
Regards...
Michael
--
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php