A couple of days ago I was all happy-happy-joy-joy coding and devvin’ and whatnot while learning to use CodeIgniter. One thing I like to use a lot (probably to the borders of abuse) is the redirect function that CodeIgniter provides. This function lets you quickly swap from one page to another by accessing PHP’s header function. It was all gayness and gollyness until today, when out-of-the-blue i got the white-screen-of-death on the testing server:

 

white screen of death

Nothing to see here, move along!

It took me a little while to track it down to the actual redirects on my code. The craziest thing is it worked alright on my developing machine but on the server BAM!!! It went completely bonkers whenever I called those silly redirects. This fact distracted me for a while since I wasted some time first producing the debug logs and then fiddling with the .htaccess file, which, needless to say, had nothing to do with my problem. The output showed something similar to this:

Cannot modify header information – headers already sent by (output started at ~/application/controllers/item.php:1) ~/system/libraries/Session.php 671

Anywho, after some hours spent googling around I came upon these two articles which basically told me to go and look for some bloody-troll-party-pooper character which was being outputed before my redirect function took place and, as we all well know (I’m just kidding you, I just found out), once the headers are sent you cannot modify them (unless you change other stuff somewhere else)… so there I went, on a trailing-character-blank-space-at-end-of-file hunt… or something. The articles also recommend changing the encoding and line endings of the source files so they match your server’s environment.

It actually took me quite some time to remember that there had been a commit earlier when we changed the encoding for some of these files as we were having trouble somewhere else, so I opened those files up in gedit and started inspecting them…. Nope, no trailing or preceding characters anywhere to be seen (emphasis on “seen”).

Then I noticed something strange. On the commit page, thanks to the great guys from Springloops (awesome project management software which I’ll cover in another post very soon) I saw that the changeset showed a difference in my code which wasn’t really different -at least for the human eye’s perspective:

 

Springloops changeset

Yeah that's not weird at all

Then the darndest thing happened: while double checking for preceding spaces I erased the first line of those files and it came to my attention that I had to press the delete key twice before anything got erased… rather amused by this strange behavior I confirmed that there was some ghostly character at the beginning of all of the files I commited earlier which must have been some kind of carriage return or something so that no one would see it -except my testing server, of course.

 

Preceding invisible character

Ye bloody brat

After that it was just a matter of seconds after I erased those trolling-bits, commited my code and had my app working online again :D

 

memebase dreamy tears teary eyed guy

That's me after fixing this.

So kids, learn to keep your files in a standardized encoding format (preferably UTF-8) and set line endings to match those of your server or developing environment!

Happy coding :)

Update: I’m being told this ghostly character is the infamous BOM (Byte order mark) and it specifies the byte order on your text stream. Interesting!