简体   繁体   中英

php script can't send headers because headers have already been sent, but are characters outside the script

This are my first two lines:

<?php
header('Content-Type: application/x-javascript');

And it gives me the headers already sent in line 1.

It is intended to generate a JavaScript file that loads from an HTML page, when checked the JavaScript file from Firebug i got the following file:

1  <br />
2  <b>Warning</b>: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\totoro\Js\libs.php:1) in <b>C:\Program Files\Apache Group\Apache2\htdocs\totoro\Js\libs.php</b> on line <b>1</b><br />
3  var Totoro = {}, $t = Totoro;

As you can see, it spits a character that looks like garbage characters, but nothing is sended, the first line is a header function call. What might be the problem?

您很可能已将文件另存为带有BOM (字节顺序标记)的UTF-8,然后另存为UTF-8,而无需多说。

This character sequence is the UTF-8 BOM . When using UTF-8, save your files without BOM (sometimes also called signature).

Additionally you should declare the encoding you're using. Because as the UTF-8 BOM is shown as the characters you named, your data is probably interpreted with ISO 8859-1, as the UTF-8 BOM byte sequence 0xEFBBBF represents the characters ï (0xEF), » (0xBB) and ¿ (0xBF) in ISO 8859-1.

So remove the BOM and use this Content-Type header field along with the correct MIME type application/javascript and charset parameter:

Content-Type: application/javascript;charset=utf-8

I foudn this on wikipedia: http://en.wikipedia.org/wiki/Byte-order_mark#Representations_of_byte_order_marks_by_encoding

This made me change the preferences of my Komodo editor an there is a "Encoding: use signature (BOM)" checked. Once unchecked the problem is solved.

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