开发者

I get the error "Cannot modify header information" in utf8, how to overcome it?

开发者 https://www.devze.com 2023-03-26 09:38 出处:网络
The code works in ANSI perfectly but when I change the encoding to utf8 the following error appears: Warning: Cannot modify header information - headers already sent by (output started at C:\\xamppl

The code works in ANSI perfectly but when I change the encoding to utf8 the following error appears:

  • Warning: Cannot modify header information - headers already sent by (output started at C:\xampplite\htdocs\alex.php:1) in C:\xampplite\htdocs\alex.php on line 2

-

<?php 
header("Content-Type: image/png");
$im = @imagecrea开发者_Python百科te(110, 20)
    or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text Привет", $text_color);
imagepng($im);
imagedestroy($im);
?>

the output of the code must be the generated image with the sentence.

When I save it in utf8 without BOM a got completely different characters.

For example if i save it in utf8 without BOM the output of the script will be the following:

I get the error "Cannot modify header information" in utf8, how to overcome it?

But I expect this sentence: "A Simple Text Привет"


You probably have a BOM (byte-order mark) at the start of the file. Since it's at the very start, before the <?php opening tag, it's output before your PHP code starts executing. Use an editor which allows you to create and edit UTF-8 files without BOM (for example, Notepad++).


@delnan is correct.

To preserve the encoding before changing it, cut the text to the clipboard (CTRL+X), change the encoding, then paste your text back in (CTRL+V).


Use UTF-8 without BOM and paste this at the start of your PHP file:

header('Content-Type: text/html; charset=UTF-8');
0

精彩评论

暂无评论...
验证码 换一张
取 消