...

View Full Version : Resolved Warning: Cannot modify header information - headers already sent



CallumJohnson
02-16-2010, 04:36 PM
first off, i'm not sure if this is the correct subforum to submit this problem; apologies if it isn't.

The problem is:


Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\include\php_layout\admin_suite\header.php:45) in C:\xampp\htdocs\admin\admin_webmasterqueries.php on line 166

admin_suite\header.php:

include("../include/session.php");
if(!$session->isAdmin()){header("Location: ../game/index.php"); exit;}
else{ //Display everything below if user is not logged in
include("../include/php_layout/admin_suite/header.php"); //header

line 166 in delete function:

//code executes if admin decides to delete query
if($id == "delete" && $status == "delete") {

//Added for furthur security measures - [Second check that user is authorised]
if(!$session->isAdmin()){header("Location: ../game/index.php");}else{

$query_id = $_GET['queryID']; //Gets the querie's ID
$q = "DELETE FROM contactwebmaster_requests WHERE wmqueryID='$query_id' LIMIT 1";
$query_delete = $database->query($q) or die("MySQL error: ".mysql_error());;
header("Location:".$session->referrer."");}

}//if ID=delete

line 166 being:
header("Location:".$session->referrer."");}


I can't seem to figure out why its saying headers have already been sent? Any help would be seriously appreciated :)

CallumJohnson
02-16-2010, 04:46 PM
also have tried:


//code executes if admin decides to delete query
if($id == "delete" && $status == "delete" && $session->isAdmin()) {
ob_start();
header("Location:".$session->referrer.""); //UNRESOLVEDISSUE


$query_id = $_GET['queryID']; //Gets the querie's ID
$q = "DELETE FROM ".TBL_WMQUERIES." WHERE wmqueryID ='$query_id' LIMIT 1";
$database->query($q) or die("MySQL error: ".mysql_error());
ob_flush();
}//if ID=delete

but to no avail

angst
02-16-2010, 04:46 PM
you'll need to debug, it's likely that one of the includes or the sql query is throwing an error which is causing the "modify header" error.

_Aerospace_Eng_
02-16-2010, 04:51 PM
Do you have any output before your header calls? Are you calling any echos. Do you have a blank line at the top of your file? Have you checked to see if you have a BOM at the top of your file? A BOM is usually in invisible character that some editors put into code but you don't notice it.

SKDevelopment
02-16-2010, 04:51 PM
Some output has been sent to the browser before line 166. Even if you e.g. have a space before <?php at the start of the file this error would happen. Or some echo has been done before header(). Or some error message has been sent to the output stream as Angst correctly said.

Absolutely any output sent to the browser means the HTTP server has been forced to send some default HTTP headers and now sending the page body.

The good solution is to find which part of your script is sending some output before using header() and correct that.

The worse solution is to use Output Buffering functions.

Edit: _Aerospace_Eng_ has been a few seconds faster. :)

The question is very common. And most PHP forums have a sticky for this. The CF PHP forum in not an exception (http://www.codingforums.com/showpost.php?p=300877&postcount=6). Please look at it and ask questions if you do not understand something.

CallumJohnson
02-16-2010, 05:03 PM
<div id"top_blank_bar"></div>
<!--new navigation-->
<div id="navigation_container">
<ul id="sddm">
<li><a href=
<?php
if($session->isAdmin()){echo '"http://localhost/admin/admin_mailinbox.php"';}
else{echo '"http://localhost/mailinbox.php"';}?>
onmouseover="mopen('m1')"
onmouseout="mclosetime()"
class="link_image">
<?php
include_once('../include/mailicon_codes.php');
?>
</a>
<a href="#">Send Message</a>
<a href="#">View Sent</a>
<a href="#">Notifications</a>
</div></li>
<li><a href="../game/index.php">To Game</a></li>
<li><a href="#"
onmouseover="mopen('m2')"
onmouseout="mclosetime()">General</a>
<div id="m2"
onmouseover="mcancelclosetime()"
onmouseout="m

it seems i had a BOM but this line is throwing the header error too: <?php
if($session->isAdmin()){echo '"http://localhost/admin/admin_mailinbox.php"';}

the line in question being:
<?php

_Aerospace_Eng_
02-16-2010, 06:51 PM
Is your header before that line or after that line? If its after then yes it will throw the error because you use echo which is sending output to the browser. We need to see the exact error and you need to post all of your code.

CallumJohnson
02-17-2010, 12:54 PM
Admin_webmasterqueries.php


<?php //Copyright Imperiumgame.co.uk 2010

include("../include/session.php");
if(!$session->isAdmin()){header("Location: ../game/index.php"); exit;}
else{ //Display everything below if user is not admin
include("../include/php_layout/admin_suite/header.php"); //header


//This next bit is for administrators to
//view any help-desk submissions
if(!isset($_GET['id'])) {
$id = $_GET['id'];
$user = $session->username;

$q_unread = "SELECT * FROM ".TBL_WMQUERIES." ORDER BY wmqueryStatus='unread' DESC";
$query_unread = $database->query($q_unread) or die(mysql_error());

if(mysql_num_rows($query_unread) == 0){$q = "SELECT * FROM ".TBL_WMQUERIES." ORDER BY Timestamp DESC";}
else {$q = "SELECT * FROM ".TBL_WMQUERIES." ORDER BY wmqueryStatus='unread' DESC";}

$getquery = $database->query($q) or die(mysql_error());

//Notice header bar css/divs
echo '<td class="site_maincontent_tblcss" colspan="2">';
echo' <div id="main_cont_placer">
<div id="main_cont_header">
<div id="main_cont_headerleft"></div>
<div id="main_cont_headerright"></div>
<div id="main_cont_headertext">
View queries submitted to webmaster</div></div>
<div id="main_cont_content" align="center">';

echo '<div id="compose" class="container_form_css">';

if(mysql_num_rows($getquery) == 0){
echo '<strong><br /><br /><p>There has been no webmaster queries posted
'.$session->username.'</p><strong><br /><br />
</div>
<div id="main_cont_bottombg">
<div id="main_cont_bottombg_left"></div>
<div id="main_cont_bottombg_right"></div>
</div></div>

</div></td></tr>';

echo '</div>';
} else {
?>
<table border="1" style="width:580px; font-family:Arial, Helvetica, sans-serif; font-size:9px;">
<tr style="font-size:9px; background-color:#282828;">
<td colspan="2" align="center">Action</td>
<td>Query Title</td>
<td>Query Message</td>
<td>Sender IP</td>
<td>Timestamp</td>
<td>Status</td>
</tr>
</div>
<?php
echo '<form action="admin_webmasterqueries.php" method="post">';
while($query = mysql_fetch_array($getquery)){

if ($query['wmqueryStatus'] == "read"){
//displays if message(s) are 'read'?>
<tr>
<td align="center"><a href="?id=view&queryID=<?php echo $query['wmqueryID'];?>&status=load" class="link_image">
<img src="../images/global/button_archive/small_view.png" width="65" height="21" /></a></td>
<td align="center">
<a onclick="confirmation(<?php echo $query['wmqueryID'];?>)">
<img src="../images/global/button_archive/small_delete.png" width="65" height="21" /></a></td>
<td style="font-size:9px; overflow:hidden;"><?php echo $query['wmqueryTitle']; ?></td>
<td style="font-size:9px; overflow:hidden;"><?php echo $query['wmqueryText']; ?></td>
<td align="center" style="font-size:9px;"><?php echo $query['wmqueryIP']; ?></td>
<td align="center" style="font-size:9px;"><?php echo $query['Timestamp']; ?></td>
<td align="center" style="font-size:9px;">
<img src="../images/global/icon_archive/small_mail_off.png" width="30" height="21" alt="Mail Read" /></td>
</tr>

<?php
}//if ($query['wmqueryStatus'] == "read"){
else{
//displays if message(s) are 'unread'?>
<tr style="background-color:#494949; color:#FFF;">
<td align="center"><a href="?id=view&queryID=<?php echo $query['wmqueryID'];?>&status=load" class="link_image">
<img src="../images/global/button_archive/small_view.png" width="65" height="21" /></a></td>
<td align="center">
<span onclick="confirmation(<?php echo $query['wmqueryID'];?>)">
<img src="../images/global/button_archive/small_delete.png" width="65" height="21" /></span></td>
<td style="font-size:9px; overflow:hidden;"><?php echo $query['wmqueryTitle']; ?></td>
<td style="font-size:9px; overflow:hidden;"><?php echo $query['wmqueryText']; ?></td>
<td align="center" style="font-size:9px;"><?php echo $query['wmqueryIP']; ?></td>
<td align="center" style="font-size:9px;"><?php echo $query['Timestamp']; ?></td>
<td align="center" style="font-size:9px;">
<img src="../images/global/icon_archive/small_mail_on.png" width="30" height="21" alt="Mail Unread" /></td>
</tr><?php }//else unread
}

echo "</form></table>";

//Layout codes for bottom container
echo'</div>
<div id="main_cont_bottombg">
<div id="main_cont_bottombg_left"></div>
<div id="main_cont_bottombg_right"></div>
</div></div>

</div></td></tr>'; //closing tags for both
}
}//if ID

else {
$id = $_GET['id'];
$status = $_GET['status'];

//code executes if admin decides to view query
if($id == "view" && $status == "load") {

//Added for furthur security measures - [Second check that user is authorised]
if(!$session->isAdmin()){header("Location: ../game/index.php");}

$query_id = $_GET['queryID']; //Gets the querie's ID

$user = $session->username; //gets the staff's username
//Gets the corresponding details for selected query
$q = "SELECT * FROM ".TBL_WMQUERIES." WHERE wmqueryID='$query_id'";
$result = $database->query($q) or die("MySQL error: ".mysql_error());
$row = mysql_fetch_array($result);

//Notice header bar css/divs
echo '<td class="site_maincontent_tblcss" colspan="2">';
echo'<div id="main_cont_placer">
<div id="main_cont_header">
<div id="main_cont_headerleft"></div>
<div id="main_cont_headerright"></div>
<div id="main_cont_headertext">
View query:&nbsp;'.$row['wmqueryTitle'].'&nbsp;[&bull;'.$row['wmqueryID'].']</div></div>
<div id="main_cont_content" align="center">';


//Updates database that the submission has been read by a moderator
$q = "UPDATE ".TBL_WMQUERIES." SET wmqueryStatus='read' WHERE wmqueryID='$query_id'";
$database->query($q) or die("MySQL error: ".mysql_error());

?><div id="compose" class="container_form_css">
<form method="post" action="contactwebmaster_requests.php">
<table border="1" cellspacing="1" cellpadding="10" style="width:580px; font-family:Arial, Helvetica, sans-serif; font-size:9px;">

<tr><td style="width:100px; font-size:9px;"><strong>IP:</strong></td>
<td style="font-size:10px;"><?php echo $row['wmqueryIP']; ?></td></tr>
<tr><td style="width:100px; font-size:9px;"><strong>Query Subject:</strong></td>
<td style="font-size:10px;"><?php echo $row['wmqueryTitle']; ?></td></tr>
<tr><td style="width:100px; font-size:9px;"><strong>Query Message:</strong></td>
<td style="font-size:10px;"><?php echo $row['wmqueryText']; ?></td></tr>

<tr><td colspan="2" align="right" style="background-color:#2e2e2e;">
<input type="image" src="../images/global/button_archive/reply_toquery.png" name="queryAction" value="Reply" /></td></tr>

</table></form></div>
<?php }//if ID=view


//code executes if admin decides to delete query
if($id == "delete" && $status == "delete" && $session->isAdmin()) {
ob_start();header("Location:".$session->referrer.""); //UNRESOLVEDISSUE


$query_id = $_GET['queryID']; //Gets the querie's ID
$q = "DELETE FROM ".TBL_WMQUERIES." WHERE wmqueryID ='$query_id' LIMIT 1";
$database->query($q) or die("MySQL error: ".mysql_error());
ob_flush();
}//if ID=delete


//code executes if admin decides to delete query
if($id == "reply" && $status == "responce" && $session->isAdmin()) {

$query_id = $_GET['queryID']; //Gets the querie's ID
$q = "DELETE FROM ".TBL_WMQUERIES." WHERE wmqueryI ='$query_id' LIMIT 1";
$database->query($q) or die("MySQL error: ".mysql_error());
//header("Location:".$session->referrer.""); //UNRESOLVEDISSUE

}//if ID=delete


//Layout codes for bottom container (ID=view or delete or reply)
echo'</div>
<div id="main_cont_bottombg_viewquery">
<div id="main_cont_bottombg_left"></div>
<div id="main_cont_bottombg_right"></div>
</div></div>

</div></td></tr>'; //closing tags for both

}//else clause


//footer
include("../include/php_layout/admin_suite/footer.php");


} //this is the closing bracket to not admin ?>



header.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Imperium | MMORPG</title>
<link href="css/global.css" rel="stylesheet" type="text/css" />
<link href="css/main.css" rel="stylesheet" type="text/css" />
<link href="css/global_style.css" rel="stylesheet" type="text/css" />
<link href="css/navigation.css" rel="stylesheet" type="text/css" />
<!--Favicon icon goes here-->
<link rel="shortcut icon" href="../../../images/global/favicon.bmp" />
<script type="text/javascript"><!--

function confirmation(wmqueryID) {
var answer = confirm("Delete this submitted query?")
if (answer==true){
window.location = "?id=delete&queryID="+wmqueryID+"&status=delete";
return true;
}
else{
return false;
}
}

//navigation bar
var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;

// open hidden layer
function mopen(id){
// cancel close timer
mcancelclosetime();
// close old layer
if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

// get new layer and show it
ddmenuitem = document.getElementById(id);
ddmenuitem.style.visibility = 'visible';}
// close showed layer
function mclose(){
if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';}

// go close timer
function mclosetime(){
closetimer = window.setTimeout(mclose, timeout);}

// cancel close timer
function mcancelclosetime(){
if(closetimer){
window.clearTimeout(closetimer);
closetimer = null;}
}
// close layer when click-out
document.onclick = mclose;
//--></script>
</head><body>
<div id="center_container">
<table width="862" cellpadding="0" cellspacing="0" border="0">
<tr>
<td id="Site header" colspan="2" class="site_header_tblcss">

</tr>
<tr>
<td colspan="2" class="top_blank_bar_tblcss">
<div id"top_blank_bar"></div>
<!--new navigation-->
<div id="navigation_container">
<ul id="sddm">
<li><a href=
<?php
if($session->isAdmin()){echo '"http://localhost/admin/admin_mailinbox.php"';}
else{echo '"http://localhost/mailinbox.php"';}?>
onmouseover="mopen('m1')"
onmouseout="mclosetime()"
class="link_image">
<?php
include_once('../include/mailicon_codes.php');
?>
</a>
<a href="#">Send Message</a>
<a href="#">View Sent</a>
<a href="#">Notifications</a>
</div></li>
<li><a href="../game/index.php">To Game</a></li>
<li><a href="#"
onmouseover="mopen('m2')"
onmouseout="mclosetime()">General</a>
<div id="m2"
onmouseover="mcancelclosetime()"
onmouseout="mclosetime()">
<a href="admin_webmasterqueries.php">Webmaster Queries</a>
<a href="#">Staff Helpdesk</a>
<a href="#">Reported Bugs</a>
</div></li>
<li>
<a href="#"
onmouseover="mopen('m3')"
onmouseout="mclosetime()">User Control</a>
<div id="m3"
onmouseover="mcancelclosetime()"
onmouseout="mclosetime()">
<a href="admin_viewall.php">View All</a>
<a href="#">Ban User/IP</a>
<a href="#">Add/Delete/Edit</a>
<a href="#">Add/Subtract Funds</a>
<a href="#">Add/Subtract Credits</a>
<a href="#">Add/Subtract Bullets</a>
<a href="#">Give/Take Properties</a>
</div></li>
<li>
<a href="#"
onmouseover="mopen('m4')"
onmouseout="mclosetime()">Faction Control</a>
<div id="m4"
onmouseover="mcancelclosetime()"
onmouseout="mclosetime()">
<a href="#">Remove Member</a>
<a href="#">Edit Faction</a>
<a href="#">Add New Faction</a>
<a href="#">Take Control</a>
<a href="#">Add/Subtract Funds</a>
<a href="#">Add/Subtract Bullets</a>
</div></li>
<li>
<a href="#"
onmouseover="mopen('m5')"
onmouseout="mclosetime()">Game Logs</a>
<div id="m5"
onmouseover="mcancelclosetime()"
onmouseout="mclosetime()">
<a href="#">Transfer History</a>
<a href="#">Property History</a>
</div></li>
<li>
<a href="#"
onmouseover="mopen('m6')"
onmouseout="mclosetime()">Communication</a>
<div id="m6"
onmouseover="mcancelclosetime()"
onmouseout="mclosetime()">
<a href="#">Admin News</a>
<a href="#">Admin Forum</a>
</div></li>
<li>
<a href="#"
onmouseover="mopen('m7')"
onmouseout="mclosetime()">Site Statistics</a>
<div id="m7"
onmouseover="mcancelclosetime()"
onmouseout="mclosetime()">
<a href="#">Site Useage</a>
</div></li>
<li>
<a href="#"
onmouseover="mopen('m8')"
onmouseout="mclosetime()">Useful Links</a>
<div id="m8"
onmouseover="mcancelclosetime()"
onmouseout="mclosetime()">
<a href="http://localhost/phpmyadmin/index.php?db=imperium_game&token=89d6f5cf9951379a8ad93bf2c89fce5c" target="_blank">phpMyAdmin</a>
</div></li>
<li><a href="../process.php">Logout</a></li>
</ul>
<div style="clear:both"></div>
</div>
<!--/new navigation-->
</td></tr><tr>


Error message:
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\include\php_layout\admin_suite\header.php:71) in C:\xampp\htdocs\admin\admin_webmasterqueries.php on line 164

error generated when: ?id=delete&queryID=[a number corresponding to query id]&status=delete

i didn't post all of my code originally because theres loads of it :)

DaiWelsh
02-17-2010, 01:09 PM
If you have posted that correctly and "output started at C:\xampp\htdocs\include\php_layout\admin_suite\header.php:71" then I asusme there is some output buffering somewhere, because output should have started at line 1 of header.php?

Anyway the point is you are trying to a redirect


header("Location:".$session->referrer."")

but you have already sent output to the browser from the header.php file you included at the top. You either need to move your redirect logic above all output or start output buffering at the top with ob_start() so that nothing actually goes to the browser until you have decided if you are going to send a redirect.

Brief reason:

HTTP reponse look like

header
header
header

content
content
content
content

with the first blank line indicating end of headers. Therefore before you send any content the headers have to be sent. Therefore as soon as the first content is sent PHP puts out all currently set headers and once that is done there is no way to "retrofit" a new header such as "location:". By enabling output buffering nothing is sent to the browser until you tell it to or the script exits (default behaviour sends content in chunks as it is generated) and therefore PHP can insert more headers into the top of the response right up until the content is sent.

HTH,

Dai

CallumJohnson
02-17-2010, 01:33 PM
THANK YOU!

the problem was resolved by just adding:

<?php ob_start(); ?>

On line one of header.php

it now works like a dream.
Thank you all for your help, it is greatly appreciated :)

CJ

_Aerospace_Eng_
02-17-2010, 07:39 PM
That isn't really a solution. Its just a quick fix. You can usually use headers normally as long as they are at sent before any output.

CallumJohnson
02-18-2010, 12:10 PM
how come it isn't a solution?

Will i encounter more problems down the line?

DaiWelsh
02-18-2010, 02:07 PM
It is not really good practice to use ob_start() as a means to avoid issues like this as it could mask other issues, prevent you from controlling output as you want elsewhere etc.

The "better" option as Aerospace suggest is to rearrange the code so that there is no output until you know whether you want it or not.

That said, if you want to minimise refactoring and are prepared to accept the small risk that it may confuse you or other developers at some point in the future, I can't see any real problem with it, just lazy/bad/impure/whatever.

In other words an experienced developer with the time to do the job properly would refactor the code to avoid it, a less experienced developer and/or one who just needs to get it working now would not ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum