-1

我在我当前的一个项目中面临一个问题。只是分享是否有人对此类问题有经验。当我尝试使用以下脚本时:

header('Content-Type: image/jpeg');

以下错误显示:

警告:无法修改标头信息 - 标头已由第 89 行 /location/controller/Captcha.php 中的(输出开始于 /location/test.php:1)发送

我用过ob_clean();在header('Content-Type: image/jpeg'); 清理缓冲区的功能。但没有工作。我也在检查 php 文件中的任何输出或换行符,但什么也没找到。

有人可以给我一个解决方案吗?

4

2 回答 2

1

基本上,当 php 标记中存在任何空格或换行符或在标头函数之前打印的任何内容时,就会发生此类问题。但在我的项目问题中,我的所有 php 文件都使用ANSI 编码保存,除了 test.php 文件。test.php 文件以utf-8 编码保存。

而 captcha.php 文件试图访问自动打印一些特殊字符的 test.php(我不知道为什么)。

当我将编码转换为 test.php 的 ANSI 时,我的项目问题解决了。

感谢 Fred 的回复。

于 2013-08-18T07:50:16.387 回答
0

<?php在没有看到完整代码的情况下,您可能在标签、HTML 或其他类型的“输出”之外或上方有空格,甚至可能是字节顺序标记

<?php

echo "Correct";
// or vvv, but not with echo
// header("Location: http://www.example.com/");

?>

被视为“输出”的示例(在标题之前):

(space)
(space) <?php

echo "Incorrect";
header("Location: http://www.example.com/");

?>

<div align="center">Text</div> <!-- HTML -->
<!-- comment tags also considered as output -->
<?php

echo "Incorrect";
header("Location: http://www.example.com/");

?>

(byte order mark) // aka BOM which is invisible but still present
<?php

echo "Incorrect";
header("Location: http://www.example.com/");

?>

有关该header()功能的更多信息,请访问 PHP.net 网站:

于 2013-08-17T16:15:26.663 回答