Click to See Complete Forum and Search --> : Cannot Modify Header Information


StrangelyBrown
03-18-2004, 07:14 PM
Hello again,

I'm sure many of you have seen this error message a dozen times before, but I'm still confused by it - even after searching on Google and here:

Warning: Cannot modify header information - headers already sent by (output started at /home/realms/www/www/user.php:2) in /home/realms/www/www/user.php on line 45

I have tracked down the offending line of code:

header(sprintf("Location: %s", $insertGoTo));

I simply don't know how to fix this problem. My apologies for posting lots of code, but I think it's the only way. Here is my entire page (user.php):


<?php require_once('Connections/connTraveller.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}

$editFormAction = $HTTP_SERVER_VARS['PHP_SELF'];
if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
$editFormAction .= "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
}

if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "formCreateCharacter")) {
$insertSQL = sprintf("INSERT INTO characters (userID) VALUES (%s)",
GetSQLValueString($HTTP_POST_VARS['userID'], "int"));

mysql_select_db($database_connTraveller, $connTraveller);
$Result1 = mysql_query($insertSQL, $connTraveller) or die(mysql_error());

$insertGoTo = "newcharacter.php";
if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $HTTP_SERVER_VARS['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

$colname_rsUsers = "1";
if (isset($HTTP_GET_VARS['ID'])) {
$colname_rsUsers = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['ID'] : addslashes($HTTP_GET_VARS['ID']);
}
mysql_select_db($database_connTraveller, $connTraveller);
$query_rsUsers = sprintf("SELECT * FROM users WHERE ID = %s", $colname_rsUsers);
$rsUsers = mysql_query($query_rsUsers, $connTraveller) or die(mysql_error());
$row_rsUsers = mysql_fetch_assoc($rsUsers);
$totalRows_rsUsers = mysql_num_rows($rsUsers);

mysql_select_db($database_connTraveller, $connTraveller);
$query_rsCharacters = "SELECT * FROM characters";
$rsCharacters = mysql_query($query_rsCharacters, $connTraveller) or die(mysql_error());
$row_rsCharacters = mysql_fetch_assoc($rsCharacters);
$totalRows_rsCharacters = mysql_num_rows($rsCharacters);
?>
<html>
<head>
<title>Traveller Online - User Area</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_displayStatusMsg(msgStr) { //v1.0
status=msgStr;
document.MM_returnValue = true;
}

/*function chooseName()
{
var name = prompt("Please name your character.","");
if (name == null ){ // Cancel pressed
alert("Please enter a character name.");
}
else if (name == "" || name == " "){ // OK pressed but no text
alert("Please enter a valid name.");
}
else
{// OK pressed and theres something entered
alert("Your character is now called: "+ name);
}

//{ // Insert details into CHARACTER TABLE and associate CHARACTER with USER ID


//} */
//-->
</script>
</head>



<body>

<table width="787" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="256" rowspan="3" valign="top"><img src="Images/traveller.gif" width="256" height="62"></td>
<td width="104" height="29">&nbsp;</td>
<td width="48">&nbsp;</td>
<td width="379" rowspan="3" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="379" height="62"><table width="100%" border="0">
<tr>
<td>
<div align="center"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><a href="newuser.php">New
User</a></font></div></td>
<td>
<div align="center"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Forums</font></div></td>
</tr>
<tr>
<td>
<div align="center"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><a href="about.php">About
Traveller RPG</a></font></div></td>
<td>
<div align="center"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Downloads</font></div></td>
</tr>
<tr>
<td>
<div align="center"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Help</font></div></td>
<td>
<div align="center"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Links</font></div></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td height="29" valign="top"><div align="center">
<p><font color="#FF0000" size="5" face="Verdana, Arial, Helvetica, sans-serif"><strong><em>ONLINE</em></strong></font></p>
</div></td>
<td>&nbsp;</td>
</tr>
<tr>
<td height="4"></td>
<td></td>
</tr>
</table>
<table width="787" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="787" height="49">
<div align="center">
<p>&nbsp;</p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Welcome
to the Traveller Universe, <?php echo $row_rsUsers['firstName']; ?> <?php echo $row_rsUsers['lastName']; ?>. It is currently March 11th, of the Year 2700.</font></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Current
Character: </strong>charFirstName charLastName</font></p>
</div></td>
</tr>
</table>
<table width="813" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="168" height="41">&nbsp;</td>
<td width="645">&nbsp;</td>
</tr>
<tr>
<td height="83" valign="top"><form action="<?php echo $editFormAction; ?>" method="POST" name="formCreateCharacter" id="formCreateCharacter">
<div align="center">
<input name="createCharacter" type="submit" id="createCharacter" value="Create Character">
<input name="userID" type="hidden" id="userID" value="<?php echo $HTTP_GET_VARS['ID']; ?>">
</div>
<input type="hidden" name="MM_insert" value="formCreateCharacter">
</form>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong><font color="#FF0000">Existing
Users:</font></strong> This will automatically <strong>delete</strong>
your current character.</font></p></td>
<td>&nbsp;</td>
</tr>
</table>
</body>
</html>
<?php
mysql_free_result($rsUsers);

mysql_free_result($rsCharacters);
?>



Sorry for all that, but I really don't know what to do :(

Basically, when the user has clicked the "Create Character" button on "formCreateCharacter", the value "userID" is taken from the URL (a variable being passed from the previous page) and Inserted into the Table "characters". After this, the user is automatically forwarded to the page "newcharacter.php".

Please help! Many thanks in advance for your time and trouble.

pyro
03-18-2004, 09:19 PM
You can only set headers before content is sent to the browser. This means that any header lines need to come before and HTML or lines of PHP that will echo/print content. And before someone decides to bring up output buffering, yes, I know. ;)

solavar
03-18-2004, 11:43 PM
One way round the 'Headers already sent' problem is to echo the 'refresh'.

So instead of:
header(sprintf("Location: %s", $insertGoTo));

You may try:

echo "<meta http-equiv=\"refresh\" content=\"0; url=$insertGoTo\">";
exit;

StrangelyBrown
03-19-2004, 03:09 AM
Thanks guys!

Pyro: I understand the theory behind header content output, but in my code I couldn't see any lines of HTML or PHP that echoed or printed output before the "header(sprintf...)" line. What was going wrong?

I used Solavar's workaround and it works beautifully - the "header" line was just Dreamweaver's default way of rendering the PHP code in this circumstance, so it didn't matter exactly how I got the user to "newcharacter.php"

Once again, thanks! Now it's back to the endless grind ;)

shimon
03-19-2004, 04:09 AM
It doesn't need to be html output, it can be *any* output, and that includes whitespace, which includes newlines. There may be other factors too but I can see straight off that you're sending a newline after that first closing php tag:

<?php require_once('Connections/connTraveller.php'); ?>
<?php

function GetSQLValueString//...etc....


?>


I'm not sure why you close php mode and then reopen it...but this would make more sense

<?php

require_once('Connections/connTraveller.php');

function GetSQLValueString//...etc....


?>


and it may solve your problem. At least it's a step in the right direction