[SOLVED] Headers already sent...

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
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

[SOLVED] Headers already sent...

Post by The Monkey »

Ok, big long post here.

I just finished my new redesign of my website. It works fine in the /redesign folder. I have tested it for a full week... no errors. I've hyped the opening too long to pull out now. I upload it... and damn. "Headers already sent" error.

I hope you guys can help me, because I'm up a dead end.

Here is all of my program pertaining to the problem. I would like to point out that I have tested the site in its live enviroment for a while now, and it is only occuring in index.php.

Index.php:

Code: Select all

<?php
define('IN_PHPBB', true);
	$Section = 'index';
	$Title = 'true';
	$template = 'header';
	include('includes/template.php');

	/********************************************
	*	- Welcome to Index.php Version 1.0.

	*	- This script will display several things:
			- The News
			- The Tomb of Info
			- Account (Currently, Favorite Pages)
	
	*	- Let's get started.
	********************************************/

	/********************************************
	*	- Check to see if The Index is required.
	*	- If the user wants The Index,
	*		- Print the random Welcome Message
	*		- Print the last 5 News Articles
	********************************************/

if(!$SubSection) {
	$errorcheck = 'false';
?>

	<!--- Welcome Message --->
	<img src="http://images.yolegoman.com/main/monkey.gif" align="right" valign="middle" hspace="5" vspace="5" />
	<p class="welcomeMessage">
	<?=$welcomeMessage;?>
	</p>
	<br clear="right" />
	<hr align="center" noshade />
	<br />
	<!--- Latest News --->
	<div class="subheader">&nbsp;The News:</div>

<?PHP
	// Include The News
	include('includes/library/index/TheNews.php');
?>

	<div align="center" class="subheader">
	<a href="index.php?SubSection=TheNews">
	The Archives
	</a>
	</div>

<?PHP
} else if($SubSection && !$Page) {
	
include('includes/library/generator/subsection.php');

} else if($Page) {

include('includes/library/generator/page.php');

}

	$template = 'footer';
	include('includes/template.php');
	exit();
?>
Template.php, which is called by index.php:

Code: Select all

<?php
	/********************************************
	*	- Welcome to Template.php Version 1.0.
	*
	*	- This script will take our variables and output the final result.
	*
	*	- Let's get started.
	********************************************/

if($template == 'header') {

	define('IN_PHPBB', true);
	include('includes/session.php');
	include('includes/config.php');
	include('includes/queries.php');

	$header = file('includes/library/global/header.php');
	foreach($header as $header) {
	
	$code = array(
		'{title}',
		'{redirect}',
		'{l_tombofinfo}',
		'{menu}',
		'{datacenter}',
	);

	$code_replace = array(
		$title,
		$redirect,
		$l_tombofinfo,
		$menu,
		$datacenter,
	);

	$header = str_replace($code, $code_replace, $header);
	echo $header;
	}

} // End if template = header.

if($template == 'window') {
	$window = file('includes/library/global/window.php');
	foreach($window as $window) {
	
	$code = array(
		'{redirect}',
	);

	$code_replace = array(
		$redirect,
	);

	$window = str_replace($code, $code_replace, $window);
	echo $window;
	}
}

if($template == 'footer') {

		// If there is no correct SubSection, then include the error message
if($errorcheck != 'false') {
	$error = '404';
	include('includes/error.php');
}

	$footer = file('includes/library/global/footer.php');
	foreach($footer as $footer) {
	
	$code = array(
		'{randQuote}',
	);

	$code_replace = array(
		$randQuote,
	);

	$footer = str_replace($code, $code_replace, $footer);
	echo $footer;
	}
mysql_close();
} // End the Footer
?>
session.php (My session.php, not phpBB2's:

Code: Select all

<?php
define('IN_PHPBB', true);
	$site_root_path = '/home/yolegoma/public_html/';
	$phpbb_root_path2 = '/monkeyslair/';
	$phpbb_root_path = $site_root_path . $phpbb_root_path2;
	include($phpbb_root_path . 'extension.inc');
	include($phpbb_root_path . 'common.php');

	$userdata = session_pagestart($user_ip, PAGE_INDEX);
	init_userprefs($userdata);

	// Name our phpBB related variables

	$loggedin = $userdata['session_logged_in'];
	$username = $userdata['username'];
	$uservisit = $userdata['user_lastvisit'];
	$userid = $userdata['user_id'];

	// Determine if user is the administrator/ nonsense administrator

	$admin = 'false';
	$nonsenseadmin = 'false';

		if($username == 'Yolegoman') { 
	$admin = 'true'; 
		}

		if($username == 'Motfilms') { 
	$nonsenseadmin = 'true'; 
		}

	// Determine how many new posts since last visit
	// $new_post = the number of posts since last visit
	$new_post = '0';
	$result = queryDB("SELECT * FROM monkey_posts WHERE post_time >= $uservisit");
		while($row = mysql_fetch_array($result)) {
			$new_post = $new_post + 1;
		}
?>
queries.php:

Code: Select all

<?php
<?PHP
	/********************************************
		-	Welcome to queries.php Version 1.0. 
		-	This script will query our database for us.
	********************************************/

	//***** ----- The SELECT Queries ----- *****\\

	// The Title query.
	$titleResult = queryDB("SELECT * FROM site_random WHERE random_type = 'title' ORDER BY RAND() LIMIT 1");
	$titleRow = mysql_fetch_array($titleResult);
	$randTitle = $titleRow['random_text'];

	if($Section == 'index' && !$SubSection) {
		$title = 'Welcome to Yolegoman.com :: ' .$randTitle;
	} else {
		if(!$SubSection) {
				$title = $Name;
		} else if($Page) {
			$title = $Page;
		} else {
			$title = $SubSection;
		}
		$title = 'Yolegoman.com :: ' .$title;
	}

	// The Welcome Message Query
	$welcomeResult = queryDB("SELECT * FROM site_random WHERE random_type = 'welcome' ORDER BY RAND() LIMIT 1");
	$welcomeRow = mysql_fetch_array($welcomeResult);
	$welcomeMessage = $welcomeRow['random_text'];

	// The Random Quote Query
	$quoteResult = queryDB("SELECT * FROM site_random WHERE random_type = 'quote' ORDER BY RAND() LIMIT 1");
	$quoteRow = mysql_fetch_array($quoteResult);
	$randQuote = $quoteRow['random_text'];

	//*** If/Else statements ***\\

	// The Logo_TombofInfo link
	if($SubSection == 'TombofInfo' && !$Page) {
		$l_tombofinfo = 'About Yolegoman.com :: The Tomb of Info';
	} else {
		$l_tombofinfo = '<a href="index.php?SubSection=TombofInfo">';
		$l_tombofinfo .= 'About Yolegoman.com :: The Tomb of Info';
		$l_tombofinfo .= '</a>';
	}

	//*** Include Files ***\\

	// The Menu
	ob_start();
	include('includes/library/global/menu.php');
	$menu = ob_get_contents();
	ob_end_clean(); 

	// The Data center
	ob_start();
	include('includes/library/global/datacenter.php');
	$datacenter = ob_get_contents();
	ob_end_clean();
?>
header.php:

Code: Select all

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&#123;title&#125;&lt;/title&gt;

&lt;!--- The Meta Tags ---&gt;

&lt;meta name="description" content="Yolegoman.com has Lego, Brickfilms and Nonsense by Nathaniel Jones" /&gt;
&lt;meta name="keywords" content="Lego, Brickfilms, Monkey Studios, Geeky Nonsense, Yolegoman" /&gt;
&#123;redirect&#125;

&lt;!--- The Javascript ---&gt;

&lt;!--- The Style Sheet ---&gt;

&lt;style type="text/css"&gt;
		&lt;!---
		BODY &#123; scrollbar-base-color: #996633; scrollbar-track-color: #996633; scrollbar-face-color: tan; scrollbar-highlight-color: #D1BA9A; scrollbar-3dlight-color: #C4A57B; scrollbar-darkshadow-color: #BB9868; scrollbar-shadow-color: #000000; scrollbar-arrow-color: #996633;
		&#125;
		A:link, A:visited &#123; COLOR:#996633; TEXT-DECORATION: NONE; font-style: normal &#125;
		A:hover, A:active &#123; COLOR:#663300; TEXT-DECORATION: NONE; font-style: normal&#125;
		.header &#123; font-size: 19pt; font-family: "j.d."; line-height:7pt; padding-top: 8px; &#125;
		.subheader &#123; font-size:14pt; font-family: "j.d."; font-color: #663300;&#125;
		.menu &#123; font-size: 12pt; font-family: "j.d."; margin-top: 6pt; margin-bottom: 6pt;&#125;
		.submenu &#123; font-size: 11pt; font-family: "helvetica"; line-height: 14pt; margin-left: 3px;&#125;
		.pagemenu &#123; font-size: 8pt; font-family: "helvetica"; line-height: 16pt; margin-left: 13px&#125;
		.footer &#123; font-size: 8pt; font-family: "helvetica"; line-height: 11pt; font-style: italic &#125;
		.content &#123; font-size: 9pt; font-family: "helvetica"; &#125;
		.logoLink &#123; font-size: 8pt; font-family: "helvetica"; padding-top: 8px; &#125;
		.welcomeMessage &#123; font-size: 11pt; font-family: "helvetica"; margin-left: 18px; padding-top: 4px;&#125;
		.lists &#123; COLOR: #63300; font-size: 10pt; font-family: "helvetica"; padding-top: 10px; &#125;
		.font5pt &#123; font-size: 5pt; font-family: "helvetica"; &#125;
		.font6pt &#123;  font-size: 6pt; font-family: "helvetica"; &#125;
		.font7pt &#123; font-size: 7pt; font-family: "helvetica"; &#125;		
		.font8pt &#123; font-size: 8pt; font-family: "helvetica"; &#125;		
		.font9pt &#123; font-size: 9pt; font-family: "helvetica"; &#125;		
		.font10pt &#123; font-size: 10pt; font-family: "helvetica"; &#125;		
		.font11pt &#123; font-size: 11pt; font-family: "helvetica"; &#125;		
		.font12pt &#123;  font-size: 12pt; font-family: "helvetica"; &#125;
		.tinyhr &#123; color: #996633; background-color: tan; height: 3px; width: 95%; &#125;
		 hr &#123;
		  color: #996633;
		  background-color: tan;
		  height: 4px;
		  width: 95%;
		&#125;
		---&gt;
	&lt;/style&gt;
	&lt;script language="JavaScript" type="text/javascript"&gt; 
&lt;!---
function open_win(what_link,the_x,the_y,toolbar,addressbar,directories,statusbar,menubar,scrollbar,resize,history,pos,wname)&#123; 
var the_url = what_link;
the_x -= 0;
the_y -= 0;
var how_wide = screen.availWidth;
var how_high = screen.availHeight;
if(toolbar == "0")&#123;var the_toolbar = "no";&#125;else&#123;var the_toolbar = "yes";&#125;
if(addressbar == "0")&#123;var the_addressbar = "no";&#125;else&#123;var the_addressbar = "yes";&#125;
if(directories == "0")&#123;var the_directories = "no";&#125;else&#123;var the_directories = "yes";&#125;
if(statusbar == "0")&#123;var the_statusbar = "no";&#125;else&#123;var the_statusbar = "yes";&#125;
if(menubar == "0")&#123;var the_menubar = "no";&#125;else&#123;var the_menubar = "yes";&#125;
if(scrollbar == "0")&#123;var the_scrollbars = "no";&#125;else&#123;var the_scrollbars = "yes";&#125;
if(resize == "0")&#123;var the_do_resize =  "no";&#125;else&#123;var the_do_resize = "yes";&#125;
if(history == "0")&#123;var the_copy_history = "no";&#125;else&#123;var the_copy_history = "yes";&#125;
if(pos == 1)&#123;top_pos=0;left_pos=0;&#125;
if(pos == 2)&#123;top_pos = 0;left_pos = (how_wide/2) -  (the_x/2);&#125;
if(pos == 3)&#123;top_pos = 0;left_pos = how_wide - the_x;&#125;
if(pos == 4)&#123;top_pos = (how_high/2) -  (the_y/2);left_pos = 0;&#125;
if(pos == 5)&#123;top_pos = (how_high/2) -  (the_y/2);left_pos = (how_wide/2) -  (the_x/2);&#125;
if(pos == 6)&#123;top_pos = (how_high/2) -  (the_y/2);left_pos = how_wide - the_x;&#125;
if(pos == 7)&#123;top_pos = how_high - the_y;left_pos = 0;&#125;
if(pos == 8)&#123;top_pos = how_high - the_y;left_pos = (how_wide/2) -  (the_x/2);&#125;
if(pos == 9)&#123;top_pos = how_high - the_y;left_pos = how_wide - the_x;&#125;
if (window.outerWidth )&#123;
var option = "toolbar="+the_toolbar+",location="+the_addressbar+",directories="+the_directories+",status="+the_statusbar+",menubar="+the_menubar+",scrollbars="+the_scrollbars+",resizable="+the_do_resize+",outerWidth="+the_x+",outerHeight="+the_y+",copyhistory="+the_copy_history+",left="+left_pos+",top="+top_pos;
wname=window.open(the_url, wname, option);
wname.focus();
&#125;
else
&#123;
var option = "toolbar="+the_toolbar+",location="+the_addressbar+",directories="+the_directories+",status="+the_statusbar+",menubar="+the_menubar+",scrollbars="+the_scrollbars+",resizable="+the_do_resize+",Width="+the_x+",Height="+the_y+",copyhistory="+the_copy_history+",left="+left_pos+",top="+top_pos;
if (!wname.closed &amp;&amp; wname.location)&#123;
wname.location.href=the_url;
&#125;
else
&#123;
wname=window.open(the_url, wname, option);
//wname.resizeTo(the_x,the_y);
wname.focus();
wname.location.href=the_url;
&#125;
&#125;
&#125;
// --&gt; 
&lt;/script&gt;
	&lt;/head&gt;
	&lt;body text="#663300" link="#996633" vlink="#996633" alink="#663300" 							
	bgcolor="#C7A363" background="http://images.yolegoman.com/global/background.jpg"&gt;

	&lt;a name="top"&gt;&lt;/a&gt;
	&lt;br /&gt;
		&lt;!--- The Logo Program ---&gt;
&lt;table border="5" align="center" cellspacing="0" cellpadding="0" bgcolor="#99633" 
background="http://images.yolegoman.com/global/tablebg.jpg" bordercolor="tan"&gt;
	&lt;tr&gt;
		&lt;td&gt;

		&lt;!--- The Top of the Logo ---&gt;

			&lt;table border="0" align="left" cellspacing="0" cellpadding="0" 
background="http://images.yolegoman.com/global/logo_top.jpg" height="69" width="500"&gt;
				&lt;tr&gt;
					&lt;td&gt;
&lt;map name="logo"&gt;
&lt;area shape=rect coords="0,0 130,70" href="http://www.yolegoman.com/index.php"
title="Yolegoman.com | All Your Nonsense Are Belong To Us" alt="Yolegoman.com | All Your Nonsense Are Belong To Us"&gt;
&lt;area shape=rect coords="230,37 490,67" href="http://www.yolegoman.com/index.php"
title="Yolegoman.com | All Your Nonsense Are Belong To Us" alt="Yolegoman.com | All Your Nonsense Are Belong To Us "&gt;
&lt;/map&gt;

&lt;img src="http://images.yolegoman.com/blank.gif" border="0" height="60" width="500" ISMAP USEMAP="#logo"&gt;
					&lt;/td&gt;
				&lt;/tr&gt;
			&lt;/table&gt;
		
		&lt;!--- The Bottom of the Logo ---&gt;
&lt;br clear="left"&gt;
			
			&lt;table border="0" align="left" cellspacing="0" cellpadding="0" 
background="http://images.yolegoman.com/global/logo_bottom.jpg" height="30" width="500"&gt;
				&lt;tr&gt;
					&lt;td align="right" class="logoLink"&gt;
&lt;a href="http://monkeyslair.yolegoman.com"&gt;Monkey's Lair&lt;/a&gt; &amp;nbsp;|&amp;nbsp; &#123;l_tombofinfo&#125;&amp;nbsp;&amp;nbsp;&amp;nbsp;
					&lt;/td&gt;
				&lt;/tr&gt;
			&lt;/table&gt;
		&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;
&lt;br clear="left" /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;!-- The Menu Program ---&gt;
&lt;table align="left" border="0" width="16%" cellpadding="0"&gt;
	&lt;tr&gt;
		&lt;td&gt;

		&lt;table align="left" border="5" bgcolor="tan" bordercolor="tan" 
			background="http://images.yolegoman.com/global/tablebg.jpg" cellpadding="5" width="100%"&gt;
			&lt;tr&gt;
				&lt;td class="menu" nowrap&gt;
				&lt;hr class="tiny" noshade /&gt;
				&#123;menu&#125;
				&lt;hr class="tiny" noshade /&gt;
				&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/table&gt;

		&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt;
&lt;br /&gt;
&lt;br /&gt;
&#123;datacenter&#125;
		&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;
&lt;!--- The Spacer Table ---&gt;
&lt;table align="left" width="8%" border="0"&gt;
	&lt;tr&gt;&lt;td&gt; &lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;!--- The Content Table ---&gt;
&lt;table align="left" width="60%" border="5" bgcolor="tan" bordercolor="tan" background="http://images.yolegoman.com/global/tablebg.jpg" cellpadding="10"&gt;
	&lt;tr&gt;
		&lt;td class="content"&gt;
The error:

Warning: Cannot modify header information - headers already sent by (output started at /home/yolegoma/public_html/index.php:1) in /home/yolegoma/public_html/monkeyslair/includes/sessions.php on line 182

Warning: Cannot modify header information - headers already sent by (output started at /home/yolegoma/public_html/index.php:1) in /home/yolegoma/public_html/monkeyslair/includes/sessions.php on line 183


What do I do now? I'm running an unmodded phpBB2 script.

- The Monkey
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Search this forum or google for 'php headers already sent', it happens about 10 times a day :o
But just for old times sake ... it means you have output before your header call and you cannot have any output before sending headers. Output also includes blanks lines, whitespace etc.. as your error says 'output started at /home/yolegoma/public_html/index.php:1' then it looks like you might have a blank line or space before your <?php tag in index.php (and that would be considered output).
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Ha ha. Simple error really. Remove the first line from your index.php file as a blank line is interpreted and sent on to the browser.

Turn:

Code: Select all

<?php

define('IN_PHPBB', true);
into:

Code: Select all

<?php
define('IN_PHPBB', true);
Edit: Dammit ~markl999!! Give someone else a chance to sound smart :wink:
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post by The Monkey »

I posted my code exactly as it is. I knew about the blank spaces, and removed all of them, including the comments, too. No cigar.

I would like to point out, however, that I am getting another error:


Warning: Cannot modify header information - headers already sent by (output started at /home/yolegoma/public_html/index.php:1) in /home/yolegoma/public_html/monkeyslair/includes/sessions.php on line 293

Warning: Cannot modify header information - headers already sent by (output started at /home/yolegoma/public_html/index.php:1) in /home/yolegoma/public_html/monkeyslair/includes/sessions.php on line 294


Same error, different line. Hmm.

Again, it only takes place in index.php, yet index.php and the other .phps' are exactly alike. And the error is completely random. So is the numbers of the line it takes place on. Programming sux sometimes. :)

- Yolegoman, who again, has no blank space and has triple checked everything
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

lol

read up on ob_start()
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post by The Monkey »

I feel a bit like a noob, and bit not. I was right. There was no VISIBLE spaces or tabs. But what I WASN'T counting on was a lame text editor.

Instead of fooling with it with edit++, I opened in in file manager, a web based form thingy. BAM! a tab right at the beginning of index.php!

That's really frustrating, but I'm glad I got it sorted out. Whew!

- Yolegoman
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

That has happened to me a couple times a long time ago.

How annoying. ;)
Post Reply