-1

I'm getting the error Warning: "Cannot modify header information - headers already sent by (output started at /admin3/public/new_admin.php:7) in /admin3/includes/functions.php on line 10"

The output it's referring to is the code directly below. I'm not sure what part it's referring to. If the 7 at the end means line 7, then that matches up with the line I open the php code at. I call functions.php (the second part the error refers to) at the absolute top of the main page that this code lives on.

<?php require_once("../includes/session.php"); ?>
<?php require_once("../includes/db_connection.php"); ?>
<?php require_once("../includes/functions.php"); ?>
<?php require_once("../includes/validation_functions.php"); ?>
<?php error_reporting(E_ALL); ini_set('display_errors', '1'); ?>

<?php 
if (isset($_POST['submit'])) {
$required_fields = array("author", "body");
validate_presences($required_fields);

if (empty($errors)) {
    $author = mysql_prep($_POST['author']);
    $body = mysql_prep($_POST['body']); 
    $page_name = ($_POST['page_name']);

    $query  = "INSERT INTO comments (";
    $query .= "  author, body, page_name";
    $query .= ") VALUES (";
    $query .= "  '{$author}', '{$body}', '{$page_name}'";
    $query .= ")";
    $result = mysqli_query($connection, $query);

    if ($result) {
        redirect_to("new_admin.php");
    } else {
            // Failure
            $_SESSION["message"] = "There was an error that prevented the comment from being saved.";
    }
}
} else {
    $author = "";
    $body = "";
}
?>

Here's the functions I use in the above code. The "line 10" code is "redirect_to" function:

    function mysql_prep($string) {
    global $connection;

    $escaped_string = mysqli_real_escape_string($connection, $string);
    return $escaped_string;
}

    function redirect_to($new_location) {
    header("Location: " . $new_location);
    exit;
}

$connection is what I use for my database connection, which I've double checked is working

I'm not sure how to troubleshoot this, I've used this function before in a similar way, so I don't know if there is a silly error or something I'm not noticing. Thanks for any help!!

Phil
  • 157,677
  • 23
  • 242
  • 245
  • possible duplicate of [Headers already sent by PHP](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) – Phil Jan 10 '14 at 01:19
  • 1
    If your ` – Phil Jan 10 '14 at 01:21
  • I'll add in the stuff above it to my main question now – ScarletLark Jan 10 '14 at 01:23
  • @Phil is correct. Unless you are buffering output you are implicitly sending the content which is before the opening ` – Starson Hochschild Jan 10 '14 at 01:24
  • 1
    I won't put this as an answer (there's a perfectly good one on the duplicate question) but don't open and close your PHP tags like that. An empty line after `?>` will be sent as output. Simply put it all in one `` block – Phil Jan 10 '14 at 01:26
  • Like this - http://pastebin.com/s288m45j – Phil Jan 10 '14 at 01:32
  • I took out the white space like you and the link said, and that fixed it. So that's good, I'm just surprised because I've used this function before, with a similarly formatted php code, and it sent just fine, so I don't understand why the white space would be a problem now (I won't include empty lines in the future, I'm just saying it's weird I didn't run into this issue until now so I learned some bad habits I guess...) – ScarletLark Jan 10 '14 at 01:36
  • @ScarletLark You probably had `output_buffering` enabled previously – Phil Jan 10 '14 at 01:37
  • I guess. Like I said, I'm pretty new, so if it was enabled, I guess I wouldn't have noticed. Is it a good thing to enable? or are there downsides to it? (sorry if this is off topic) – ScarletLark Jan 10 '14 at 01:41
  • Whether or not it is a *good thing* is subjective. You should always code as if it is disabled – Phil Jan 10 '14 at 01:47

1 Answers1

0

you get that error when the output is already sent to the client. make sure that the header() function is called before any output is sent... Which includes any spaces, new lines or any print or echo statements. Make sure that you call the header() function before all of your HTML and php output.

If you have multiple PHP blocks before your HTML, make sure that there is no line between the two..

As you see, in your case, before line 7 and 5, there is a new line, which is provided as some output, which prevents you to modify header info. to get rid of this, simply delete the new line:

<?php require_once("../includes/session.php"); ?>
<?php require_once("../includes/db_connection.php"); ?>
<?php require_once("../includes/functions.php"); ?>
<?php require_once("../includes/validation_functions.php"); ?>
<?php error_reporting(E_ALL); ini_set('display_errors', '1'); ?>
<?php 
if (isset($_POST['submit'])) {
$required_fields = array("author", "body");
validate_presences($required_fields);

if (empty($errors)) {
    $author = mysql_prep($_POST['author']);
    $body = mysql_prep($_POST['body']); 
    $page_name = ($_POST['page_name']);

    $query  = "INSERT INTO comments (";
    $query .= "  author, body, page_name";
    $query .= ") VALUES (";
    $query .= "  '{$author}', '{$body}', '{$page_name}'";
    $query .= ")";
    $result = mysqli_query($connection, $query);

    if ($result) {
        redirect_to("new_admin.php");
    } else {
            // Failure
            $_SESSION["message"] = "There was an error that prevented the comment from being saved.";
    }
}
} else {
    $author = "";
    $body = "";
}
?>

This should work... In future, just make sure that there is no whitespace between php blocks if you're redirecting...

Anshu Dwibhashi
  • 4,617
  • 3
  • 28
  • 59
  • So even though this was thoroughly discussed in the comments and a duplicate question identified, you felt yet another answer was required? – Phil Jan 10 '14 at 01:46
  • sorry, didn't read comments, i just read the error and when i saw some whitespace i dashed to answer.. – Anshu Dwibhashi Jan 10 '14 at 01:47