Page 1 of 1

[SOLVED] Headers Already Sent -- NOT!!!

Posted: Tue Jan 20, 2004 5:57 pm
by llanitedave
I'm trying to get what I thought was a fairly simple login script to work. I have an include file, "init_db.php" which has the following code:

Code: Select all

<?php
// init_db.php
$db_database = "myDatabase";

/**********************
Describe the function genericConnect
**********************/
function genericConnect($db_database){
    $db_host = "localhost";
    $db_user = "genericLogin";
    $db_password = "uncrackable";
    $db_connection = mysql_connect($db_host, $db_user, $db_password);
    mysql_select_db($db_database);
    return $db_connection;
} // end genericConnect
?>
I use this file to logon to my database and check user logins against their name and password, and to assign them an authorization level.

If the login is a valid one, I assign session variables(Session_start is called at the top of the script):

$_SESSION['username'] = xxx;
$_SESSION['authorization'] = ...

Then I redirect my page back to itself so that it can catch the session variables and display the proper content depending on the user:

Code: Select all

header("Location: http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/"."index.php");
When I run the code, I get the following error:


Warning: Cannot modify header information - headers already sent by (output started at .../phpincludes/init_db.php:12) in .../index.php on line 71

The problem is, I can't find anything in this init_db.php that might qualify as "output". Is there another place that I should redirect my attention?

Thanks all!

Posted: Tue Jan 20, 2004 6:03 pm
by markl999
The output could be coming from a mysql error, so change:

$db_connection = mysql_connect($db_host, $db_user, $db_password);
mysql_select_db($db_database);

to :

$db_connection = mysql_connect($db_host, $db_user, $db_password) or die(mysql_error());
mysql_select_db($db_database) or die(mysql_error());

Posted: Tue Jan 20, 2004 6:16 pm
by infolock
there is a nicely written article on this subject here :

viewtopic.php?t=1157

might be just what you need ;)

Posted: Tue Jan 20, 2004 6:54 pm
by Straterra
Also, make sure there are NO spaces, no lines, NOTHING being printed to the browser. Something as simple as the script starting on line 2 classifies as output, because line one is set to the browser. Doesn't matter that it's blank, because its still sent to the browser.

Posted: Tue Jan 20, 2004 7:29 pm
by Bill H
The problem is here:

Code: Select all

<?php
// init_db.php 
$db_database = "myDatabase"; 

/********************** 
Describe the function genericConnect 
**********************/ 
function genericConnect($db_database){ 
    $db_host = "localhost"; 
    $db_user = "genericLogin"; 
    $db_password = "uncrackable"; 
    $db_connection = mysql_connect($db_host, $db_user, $db_password); 
    mysql_select_db($db_database); 
    return $db_connection; 
} // end genericConnect 
?>
The skipped line right after

Code: Select all

<?php
$db_database = "myDatabase"; 
?>
counts as output !

Posted: Tue Jan 20, 2004 7:40 pm
by jason
Bill H wrote:The problem is here:

Code: Select all

<?php
// init_db.php 
$db_database = "myDatabase"; 

/********************** 
Describe the function genericConnect 
**********************/ 
function genericConnect($db_database){ 
    $db_host = "localhost"; 
    $db_user = "genericLogin"; 
    $db_password = "uncrackable"; 
    $db_connection = mysql_connect($db_host, $db_user, $db_password); 
    mysql_select_db($db_database); 
    return $db_connection; 
} // end genericConnect 
?>
The skipped line right after

Code: Select all

<?php
$db_database = "myDatabase"; 
?>
counts as output !
No, it doesn't.

I for one would like to see the rest of init_db.php. Maybe a link to a .phps of it?

Posted: Tue Jan 20, 2004 8:12 pm
by Bill H
In the article written by Jason which is referenced by infolock above:
Here, the problem is in databaseInit.php, and it has to do with the blank lines. The blank lines before the code starts, and after the code end - both will cause problems. Remember, NO output whatsover, blank lines included!
Ah, I see my misperception -- it does say "before the code starts."

There does, however seem to be a blank line before the code begins in the OP script.

Posted: Wed Jan 21, 2004 2:30 am
by Dr Evil
Ah, I see my misperception -- it does say "before the code starts."
You can have as many blank lines as long as they are within the <?php ?> tags.
On the other hand blank lines outside those tags are considered as html and are sent to the browser... giving you an error.

Dr Evil

Posted: Wed Jan 21, 2004 3:30 am
by twigletmac
The error does tell you:
output started at .../phpincludes/init_db.php:12
So, like Jason, I would be interested in seeing at least the first 15 lines of init_db.php.

Mac

Posted: Wed Jan 21, 2004 9:29 am
by llanitedave
I for one would like to see the rest of init_db.php. Maybe a link to a .phps of it?
Actually, what I posted WAS all of init_db.php.

But it's OK. I followed Infolock's link, read your posting, Jason, and put ob_start();
at the top of my code.

Worked like a charm!

I never did find any lines that might be considered output in that file. Maybe it was somewhere else in the file that inlcuded it.

I remember reading about buffer outputs in the manual and my PHP book, but since I didn't understand it, I just lazily skimmed over it. Just goes to show ya! It just seems too easy! What's the catch?

Anyway, I'll keep posting questions, just because I like those cute avatars!