0

I am getting the following error when i want to add an article with admin page for content management:

"Warning: Cannot modify header information - headers already sent by (output started at cms/includes/connection.php:12) in cms/admin/add.php on line 23"

Could you please help me with this one ? I know that this is a frequent question here but i dont have enough experience to solve this by myself. Thank you.

This is my file connection.php

<?php

ini_set("display_errors", "1");
error_reporting(E_ALL);
try{
    $pdo = new PDO('mysql:host=localhost; dbname=cms', 'root','root');
} catch (PDOException $e) {
    exit('Database error.');
}

?>

This is my add.php file

<?php

session_start();

include_once ('../includes/connection.php');

if (isset($_SESSION['logged_in'])) {
    if(isset($_POST['title'],$_POST['content'])) {
        $title = $_POST['title'];
        $content = $_POST['content'];

        if(empty($title) or empty($content)) {
            $error = 'All fields are required!';
        } else {
            $query = $pdo->prepare('INSERT INTO articles (article_title, article _content, article_timestamp) VALUES (?,?,?)');

            $query->bindValue(1,$title);
            $query->bindValue(2,$content);
            $query->bindValue(3,time());

            $query->execute();

            header('Location: index.php');
        }

    }

      ?>

 <html lang="en">
 <head>
     <title>My Fitness Lifestyle</title>

     <link rel="stylesheet" type="text/css" href="../assets/style.css">
 </head>

 <body>

 <div class="container">
     <a href="index.php" id="logo">CMS</a>


     <br />

     <h4>Add Article</h4>

     <?php if (isset($error))  { ?>
     <small style="color:#900010;"><?php echo $error; ?>
         <br /><br />
         <?php } ?>

     <form action="add.php" method="post" autocomplete="off">
         <input type="text" name="title" placeholder="Title" /><br> <br />
         <textarea rows="15" cols="50" placeholder="Content" name="content"></textarea><br /><br />
        <input type="submit" value="Add Article" />
     </form>

 </div>
 </body>
 </html>

    <?php

}   else {
    header('Location: index.php');
}

?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Adam Beňko
  • 221
  • 3
  • 4
  • 11
  • Unless you are using `output buffering` then as soon as you send any html content you will get this error if you use `header` – Professor Abronsius Jan 16 '18 at 07:50
  • 1
    It sounds like you have a whitespace at the end of your ```connection.php```. If it's the case the output will be startet and the headers can't be modified anymore. – Jonathan Jan 16 '18 at 07:54
  • 1
    https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php?rq=1 might be of interest ~ particularly the accepted answer as it has lots of info – Professor Abronsius Jan 16 '18 at 07:58

2 Answers2

0

Your connection.php file is giving some warning or notice, thats why you are getting this error.

you should remove this line from connection.php to avoid warnings or notice.

// ini_set("display_errors", "1");
// error_reporting(E_ALL);
Harsh Virani
  • 367
  • 3
  • 8
-1

As stated by @RamRaider, you may enable output buffering in your php.ini or you can use ob_start(); inside your php file to manually start buffering. Please keep in mind that this only works before any output has been sent.

We currently don't have any line numbers in your code so you should check what exactly can be found in line 12 of your connection.php. I assume that because of enabling error display with all available log levels, you will get an E_NOTICE or comparable which will also cause output to be served.

Example for manual output buffering

Taking your example you may enable output buffering like so:

<?php

// start buffering
ob_start();

session_start();

include_once ('../includes/connection.php');

if (isset($_SESSION['logged_in'])) {
    if(isset($_POST['title'],$_POST['content'])) {

As soon as your script is completely run, the output buffer will be automatically flushed to the client. You may manually flush content by using ob_flush(); but I haven't found a single practical use for this yet (as most browsers wait for response completion rather than handling partial responses).

Regarding Jonathan's comment about trailing content

As Jonathan stated in his comment, any trailing invisible characters after the closing PHP tag will also be sent to the client immediately causing the "header already sent" error, therefor I would recommmend omitting the closing PHP tag at the end of your script. There is a good SO question about omitting the header and why it's recommended to skip it.

SaschaM78
  • 4,376
  • 4
  • 33
  • 42
  • The 11.th line of the connection.php file is ?>, after that there is nothing, so the 11th line is the last line of the code. If I put ob_start(); at top of my php file, error disappears but the article that I want to add through the admin page wont be saved and thus no article will be added in the end – Adam Beňko Jan 16 '18 at 10:32
  • @AdamBeňko that seems to be a different problem then. First you should get rid of the closing tag as recommended. After that, check if you are redirected if you try to add an article. If it is the case, there's a high chance an error occurs while calling `$query->execute();` that is hidden by the header redirection. – SaschaM78 Jan 16 '18 at 10:43
  • I've realized that i had one blank line below the ?> tag, removing it got rid of the warning that occured. If i submit the new article, i am redirected back to the index.php which is the main admin page. What do you suggest to try next ? What should i do with $query->execute(); ? By the way, thanks a lot for the help. – Adam Beňko Jan 16 '18 at 11:00
  • I would recommend to comment the `header("Location: ...");` for a moment and change your PDO statement to `if (!$query->execute()) { print_r($query->errorInfo()); }`, then retry saving the article. The occurring error will be displayed as third element of the printed array. You're welcome :-) – SaschaM78 Jan 16 '18 at 11:28