Sunday 6 February 2011

Drupal error - warning: Cannot modify header information

Upon uploading my site from the local to the remote server an error (warning) was received when attempting to run update.php. If I ignored the warning and continued using the site I would get a blank white page every now and again on the administration pages although it did carry out the requested operation. I was having none of it and it was something that needed to be solved.

The error message I got:

warning: Cannot modify header information - headers already sent by (output started at /home/{username}/public_html/drupal/sites/all/modules/{modulename}/{modulename}.module:1) in /home/{username}/public_html/drupal/includes/common.inc on line 148.

It took a while to figure out what the problem was. The first thing to say is that the line and file number it says is causing the problem usually is the file and line number causing the problem. So I opened up the custom module I'd written and here was what it had in the first two lines:

<?php
// $Id$

Nothing strange about that. And there was no white space preceding the opening of the php code tag.

To cut a very boring story short, the problem turned out to be that I'd saved the file as UTF-8 and before the opening tag the text-editor had stuck in an invisible UTF-8 marker character which was being sent to the browser before the headers and thus causing the error.

The solution is to change the text file to ASCII (or ANSI as some text-editors call it) or find another way to remove that preceding character(s?) and if any preceding characters show up in the ASCII file then delete them and save the file.

3 comments:

  1. Thank you! Spent about three hours trying to debug my first Drupal module that was very basic, but throwing the WSOD. The log files indicated exactly what you mentioned and I changed my text file from UTF-8 to Western (ISO Latin 1) and that solved the problem. Whew!

    ReplyDelete
  2. Turns out for our site that a database table had crashed. We have GoDaddy hosting and a call to them resulted in them correcting the table within 20 seconds. Not a total GoDaddy fan but this was worth the other aggravations.

    ReplyDelete