3

As a result,functions like session_start and setcookie can't run successfully,reporting:

Cannot modify header information - headers already sent by

But the target file is like this:

1 <?php
2 session_start(); 

How to fix it?

FOUNDINGS

I've found the problem,the utf-8 formated files become utf-8+BOM after uploading to the server,so I've temporarily solved the problem by saving it as utf-8 again.

BUT,there are lots of other files with the same problem,how can I batch solve the issue?

user198729
  • 61,774
  • 108
  • 250
  • 348

7 Answers7

7

Smells like a BOM problem as some editors don't show it. Try opening your file with Notepad++, Emacs or any other editor which show it. If you have some weird character before the <, you got the culprit.

Arkh
  • 8,416
  • 40
  • 45
  • You are right,it's a BOM problem,but how to batch solve it?I don't want to re-format all files one by one... – user198729 Dec 18 '09 at 16:24
3

If your file is Unicode-encoded, a two byte BOM (Byte Order Mark) is added at the start of the file. This BOM is not displayed by most editors. For example in Notepad++, you can change the encoding to UTF-8 without BOM or just a completely different encoding like ANSI.

eflorico
  • 3,589
  • 2
  • 30
  • 41
2

One tip is to leave off the trailing ?> at the end of your PHP files – this avoids cases where you might have trailing whitespace at the end of the file.

Ciarán Walsh
  • 1,866
  • 14
  • 11
1

There's only one way to fix it: find the space! It's somewhere, it's always somewhere.

To fix, try the three steps in this article.

Derek Illchuk
  • 5,638
  • 1
  • 29
  • 29
0

can you post whole program what you tried? check this code,make sure you added ?> at end

  <?php
session_start();
session_register("count");
if(!isset($_SESSION))
{
    $_SESSION["count"]=0;
    echo "<p>counter initialized</p>\n";
}
else
{
    $_SESSION["count"]++;
}
echo "<p>The counter is now <b> $_SESSION[count]</b></p>";
echo "<p>Reload this page to increment</p>";
?>
Abhilash Muthuraj
  • 2,008
  • 9
  • 34
  • 48
0

Open the file in a good editor like vi and you will see all the nonsense characters and whitespace.

If you find the offending character and it is present many of your files you could probably use grep/xargs/sed to replace it out of all the files at once. Not sure how you do that on Windows though.

As to the answer suggesting that you leave off the closing ?> php tag... I've seen/heard this suggested several times but it just doesn't sit right with me. If you are going to omit the closing php tag from all of your files then you should put a comment at the end of every file indicating this so you know the file hasn't been truncated.

// end of file

However, if you are going to do this to all of your files you might as well just delete the whitespace after ?> tag. It's really not that hard unless you are sloppy with your coding.

Anon
  • 290
  • 2
  • 6
0

Turn on Output buffering in PHP, this can be done at the php.ini level, or in .htaccess http://www.php.net/manual/en/outcontrol.configuration.php#ini.output-buffering

Then all your output will be cached in a buffer until your script ends, and it's sent at once. This is often good from a performance perspective (as your server sends fewer, larger, chunks of data). Depending on your site architecture you may want to put a flush(); in after your HTML headers are generated.

preinheimer
  • 3,712
  • 20
  • 34