PDA

View Full Version : PHP "Cannot Modify Header Information" Error




steelphantom
Nov 5, 2009, 11:53 AM
I'm getting this PHP error on my site that is saying the following:

[phpBB Debug] PHP Notice: in file /includes/session.php on line 1006: Cannot modify header information - headers already sent by (output started at /home8/nicktib1/public_html/theleafsband/index.php:14)
[phpBB Debug] PHP Notice: in file /includes/session.php on line 1006: Cannot modify header information - headers already sent by (output started at /home8/nicktib1/public_html/theleafsband/index.php:14)
[phpBB Debug] PHP Notice: in file /includes/session.php on line 1006: Cannot modify header information - headers already sent by (output started at /home8/nicktib1/public_html/theleafsband/index.php:14)

I found this link (http://kb.siteground.com/article/Php_Error_Cannot_modify_header_information.html) which helped me "fix" the problem by adding a php.ini file, but how can I fix it for real?

Any ideas? Thanks! :)



RonCarr
Nov 5, 2009, 12:11 PM
are you sending output info before you start the session? if so that may be the reason.

steelphantom
Nov 5, 2009, 12:50 PM
I'm not sure what that means. All I'm doing with PHP on the home page is using <?php include("news.php"); ?> inside the content div to include news files from the forum. news.php is a mod scrip for PHPBB that I downloaded from the official web site.

RonCarr
Nov 5, 2009, 01:01 PM
so that is all that is in your php file? The problem may be in the external file that you are including.

steelphantom
Nov 5, 2009, 01:17 PM
so that is all that is in your php file? The problem may be in the external file that you are including.

Well, there is an include later on which includes the sidebar and the footer, but that actually works. It must be the external file, because I included a dummy file and it worked perfectly. Here's the code of the external file:


<?php

/******************************************************************************

* POST SYNDICATION SCRIPT by chAos

* A very basic script that pulls threads with the first post from the database

* and puts them into an array form so you can use them as you like.

* For use with phpBB3, freely distributable

******************************************************************************/

/** Notes:

* - Attachments haven't been handled properly.

* - Starts a forum session as Guest user, taking all the default values for time, bbcode style (from theme), etc

* - While viewing this page, users will appear to be viewing the Forum Index on viewonline.php.

* This can't be helped without modifying other code which is beyond this

*/

define('FORUM_ID', 3); // Forum ID to get data from
define('POST_LIMIT', 5); // How many to get
define('PHPBB_ROOT_PATH', './forum/'); // Path to phpBB (including trailing /)

define('PRINT_TO_SCREEN', true);

// If set to true, it will print the posts out
// If set to false it will create an array $news[] with all the following info
// 'topic_id' eg. 119
// 'topic_time' eg. 06 June, 07 (uses board default)
// 'topic_replies' eg. 26
// 'username' eg. chAos
// 'topic_title' eg. "News Post"
// 'post_text' eg. just the text (formatted w/ smilies, bbcode, etc)



//

//////////////////////////////////////

define('IN_PHPBB', true);

$phpbb_root_path = PHPBB_ROOT_PATH;

$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'common.' . $phpEx);

include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

include($phpbb_root_path . 'includes/bbcode.' . $phpEx);

// Start session management

$user->session_begin(false);

$auth->acl($user->data);

// Grab user preferences

$user->setup();

$query =
"SELECT u.user_id, u.username, t.topic_title, t.topic_poster, t.forum_id, t.topic_id, t.topic_time, t.topic_replies, t.topic_first_post_id, p.poster_id, p.topic_id, p.post_id, p.post_text, p.bbcode_bitfield, p.bbcode_uid
FROM ".USERS_TABLE." u, ".TOPICS_TABLE." t, ".POSTS_TABLE." p
WHERE u.user_id = t.topic_poster
AND u.user_id = p.poster_id
AND t.topic_id = p.topic_id
AND p.post_id = t.topic_first_post_id
AND t.forum_id = ".FORUM_ID."
ORDER BY t.topic_time DESC";

$result = $db->sql_query_limit($query, POST_LIMIT);
$posts = array();
$news = array();
$bbcode_bitfield = '';
$message = '';
$poster_id = 0;

while ($r = $db->sql_fetchrow($result))
{
$posts[] = array(
'topic_id' => $r['topic_id'],
'topic_time' => $r['topic_time'],
'username' => $r['username'],
'user_id' => $r['user_id'],
'topic_title' => $r['topic_title'],
'post_text' => $r['post_text'],
'bbcode_uid' => $r['bbcode_uid'],
'bbcode_bitfield' => $r['bbcode_bitfield'],
'topic_replies' => $r['topic_replies'],
);

$bbcode_bitfield = $bbcode_bitfield | base64_decode($r['bbcode_bitfield']);
}

// Instantiate BBCode

if ($bbcode_bitfield !== '')
{
$bbcode = new bbcode(base64_encode($bbcode_bitfield));
}

// Output the posts

foreach($posts as $m)
{
$poster_id = $m['user_id'];
$message = $m['post_text'];

if($m['bbcode_bitfield'])
{
$bbcode->bbcode_second_pass($message, $m['bbcode_uid'], $m['bbcode_bitfield']);
}
$message = str_replace("\n", '<br />', $message);
$message = smiley_text($message);
$comment = ($m['topic_replies']==1) ? 'comment' : 'comments';

if( PRINT_TO_SCREEN )
{
echo "\t\t<div class=\"post\">";
echo "\n\t\t\t<h2><a href=\"".PHPBB_ROOT_PATH."viewtopic.php?f=".FORUM_ID."&amp;t={$m['topic_id']}\">{$m['topic_title']}</a></h2>";
echo "\n\t\t\t<p class=\"content_text\">{$message}</p>";
echo "\n\t\t\t<p class=\"postedby\"> by <a href=\"".PHPBB_ROOT_PATH."memberlist.php?mode=viewprofile&amp;u={$m['user_id']}\">{$m['username']}</a> on ".date("F j, Y", $m['topic_time'] + 18000)." | <a href=\"".PHPBB_ROOT_PATH."viewtopic.php?f=".FORUM_ID."&amp;t={$m['topic_id']}\">{$m['topic_replies']} {$comment}</a></p>";
echo "\n\t\t</div>\n";
}

else
{
$news[] = array(
'topic_id' => $m['topic_id'], // eg: 119
'topic_time' => $user->format_date($m['topic_time']), // eg: 06 June, 07 (uses board default)
'topic_replies' => $m['topic_replies'], // eg: 26
'username' => $m['username'], // eg: chAos
'topic_title' => $m['topic_title'], // eg: "News Post"
'post_text' => $message, // just the text
);
}

unset($message,$poster_id);
}
?>

RonCarr
Nov 5, 2009, 01:25 PM
where is the session.php that it mentions in the error?

steelphantom
Nov 5, 2009, 01:38 PM
where is the session.php that it mentions in the error?

session.php is located at http://theleafsband.com/forums/includes/session.php

Line 1006 is:

header('Set-Cookie: ' . $name_data . (($cookietime) ? '; expires=' . $expire : '') . '; path=' . $config['cookie_path'] . $domain . ((!$config['cookie_secure']) ? '' : '; secure') . '; HttpOnly', false);


More helpful, probably, is this:

/**
* Sets a cookie
*
* Sets a cookie of the given name with the specified data for the given length of time. If no time is specified, a session cookie will be set.
*
* @param string $name Name of the cookie, will be automatically prefixed with the phpBB cookie name. track becomes [cookie_name]_track then.
* @param string $cookiedata The data to hold within the cookie
* @param int $cookietime The expiration time as UNIX timestamp. If 0 is provided, a session cookie is set.
*/
function set_cookie($name, $cookiedata, $cookietime)
{
global $config;

$name_data = rawurlencode($config['cookie_name'] . '_' . $name) . '=' . rawurlencode($cookiedata);
$expire = gmdate('D, d-M-Y H:i:s \\G\\M\\T', $cookietime);
$domain = (!$config['cookie_domain'] || $config['cookie_domain'] == 'localhost' || $config['cookie_domain'] == '127.0.0.1') ? '' : '; domain=' . $config['cookie_domain'];

header('Set-Cookie: ' . $name_data . (($cookietime) ? '; expires=' . $expire : '') . '; path=' . $config['cookie_path'] . $domain . ((!$config['cookie_secure']) ? '' : '; secure') . '; HttpOnly', false);
}

Darth.Titan
Nov 5, 2009, 01:48 PM
Your session.php file is setting a cookie. Cookies must be set before any output is sent to the browser. Make sure that the session file is being called before anything is output.

Sometimes having a blank line at the beginning of any of your PHP files will cause this.

steelphantom
Nov 5, 2009, 02:21 PM
Your session.php file is setting a cookie. Cookies must be set before any output is sent to the browser. Make sure that the session file is being called before anything is output.

Sometimes having a blank line at the beginning of any of your PHP files will cause this.

Figured it out! I needed to include the first part of the news.php at the beginning of the file, and the second part where I wanted to output the HTML. Thanks, guys! :)

RonCarr
Nov 5, 2009, 05:49 PM
Great! Glad you got it sorted out then. What the other guy was talking about was the same as what I had originally thought.