My code has a bug and I narrowed it down to this; here are the first 6 lines of my php script:
<?php
session_start();
require_once("config.php");
include_once("../common/class.database.php");
//$database = new manageDatabase();
header('Location: index.php');
When the page loads i get a ‘Cannot modify header information - headers already sent’ error. If I comment out
include_once(“…/common/class.database.php”);
the page loads without an error. But that class does not echo or print anything to the page. There is the below constructor in the class but it’s not outputting data to the browser. Unless it’s doing something I’m not aware of. What could cause this error?
function __construct(){
$this->dbOpen();
}
function dbOpen(){
if($this->dbConn = @mysql_connect(HOSTNAME, USERNAME, PASSWORD)){
mysql_select_db(DBNAME);
return true;
} else {
return false;
}
}
Thanks in advance.
If you are absolute positive nothing is output before the headers then there’s one reason for this error.
Check your editor’s settings and make sure it saves the file without BOM (Byte Order Mark).
BOM is a character used in unicode which is placed at the very beginning of a file and indicates (guess it) byte order in the file. It’s optional but browsers don’t handle it and treat is a normal output, hence the error.
but would BOM affect the fact that when I comment out the include_once(“…/common/class.database.php”); line the error goes away. And when I uncomment it, it comes back?
Yes, if …/common/class.database.php is saved with BOM but the file which includes it isn’t.
Edit:
Also, if …/common/class.database.php has an ending ?> tag and you have a new line character after it (some editors may add it automatically) it can cause the error, as anything after the tag is output to the browser.
Best practice is to not use ?> tag in files that are pure PHP (closing tag is optional at the end of a file).
You were right!!
There was space after ?> in …/common/class.database.php .
Thank You!