Jump to content

Getting "Cannot modify header information - headers already sent by" error...


yobri

Recommended Posts

... and I cannot seen to identify where in the code the error is being generated. I have checked the code to see if header information is being sent prior to line 60, but cannot identify such. Plus, I do not identify any errant whitespace in the code either.

 

Error returned:

Warning: Cannot modify header information - headers already sent by (output started at /backup/tmphome/********/public_html/testphp/index2.php:8) in /backup/tmphome/********/public_html/testphp/index2.php on line 60

 

Below is the code that I have which writes to the MySQL DB fine, but the error comes up first and then I have to hard refresh to see the generated output. Thanks for any help that any of you may be able to provide.

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<?php

$host = "********";
$user = "********";
$pass = "********";
$db = "********";

mysql_connect($host, $user, $pass) OR die ("Could not connect to the server.");
mysql_select_db($db) OR die("Could not connect to the database.");
       
$name = stripslashes($_POST['txtName']);
$message = stripslashes($_POST['txtMessage']);

if (!isset($_POST['txtName'])) {

    $query = "SELECT id, name, message, DATE_FORMAT(date, '%D %M, %Y @ %H:%i') as newdate FROM guests ORDER BY id DESC";

    $result = mysql_query($query);
     
    while ($row = mysql_fetch_object($result)) {

?>
<p><strong><?php echo $row->message; ?></strong>
<br />Posted by <?php echo $row->name; ?> on <br /><?php echo $row->newdate; ?></p>
<?php
         
    }
     
?>
<p>Post a message</p>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

    <p><label for="txtName">Name:</label><br />
    <input type="text" title="Enter your name" name="txtName" /></p>

    <p><label for="txtMessage">Your message:</label><br />
    <textarea title="Enter your message" name="txtMessage"></textarea></p>
     
    <p><label title="Send your message">
    <input type="submit" value="Send" /></label></p>
     
</form>
<?php

}

else {

    $query = "INSERT INTO guests SET message='$message', name='$name', date=NOW()";
    $result = mysql_query($query);

    $ref = $_SERVER['HTTP_REFERER'];
    header("Location:$ref");
}

?>
</body>
</html>

 

Yes, I am a noob to PHP development...  :P

So can you recommend a better way for me to code this so that the user gets returned to the page where the submission was made? This is the aim I was trying for, as all gets written to the DB fine, but I can't seem to get the user to see the output that they just submitted without running into this error.

Yes, adjust your logic so no output is sent to the browser before the Header, or don't use a header.

 

There is no reason to set all that HTML before your code if you are just going to redirect is there?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.