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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
llanitedave
Forum Commoner
Posts: 78
Joined: Thu Jan 15, 2004 11:24 am
Location: Las Vegas, NV.

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

Post 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!
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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());
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

there is a nicely written article on this subject here :

viewtopic.php?t=1157

might be just what you need ;)
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post 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.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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 !
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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?
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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.
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
llanitedave
Forum Commoner
Posts: 78
Joined: Thu Jan 15, 2004 11:24 am
Location: Las Vegas, NV.

Post 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!
Post Reply