2

Possible Duplicates:
Warning: Cannot modify header information - headers already sent by
what is output buffering?

I recently bought a hosting plan from Bluehost and moved my website here. Before, my website was working, and I haven't recieved any errors, but now, when I try to login I recieve this error:

Warning: Cannot modify header information - headers already sent by (output started at /home/rugbyspi/public_html/inc/offline_header.php:15) in /home/rugbyspi/public_html/index.php on line 84

The problem is the header function. I think I can't use this function in middle of the code, but on my other host I can do this. Any idea how to fix this problem?

<?php
session_start();
if (isset($_SESSION['username'])) {
   include_once('inc/homepage_header.php');
   include_once('lib/stats_func.php');
   include_once('lib/user_time_func.php');
   update_last_active($username);
?>

<div id="right_menu">
   <?php include_once('inc/sidebar_statistics.php'); ?>
   <br/>
   <?php include_once('inc/sidebar_follow.php'); ?>
</div>

<div id="content">
   <div class="text">
      <br/>
      <h2 class="hr"><?php echo $lang['ann']; ?></h2>
      <?php
      if(!isset($_REQUEST["start"])) {
         $start = 0;
      }
      else {
         $start = $_REQUEST["start"];
      }
      $query = mysql_query ("SELECT * from news where lang='$language'")
      or die ("Couldn't connect to the do database.");
      $d=0;
      $f=0;
      $g=1;
      $query = mysql_query ("SELECT * from news where lang='$language' order by id DESC Limit $start, 7") or die ("Couldn't connect to the do database.");
      while($news = mysql_fetch_array($query)) {
         ?>
         <h3 class="hr"><font size=1><?php echo $news['tsubmit']; ?></font>&nbsp;        &nbsp;<?php echo $news['title']; ?></h3>
         <?php
         echo $news['body'];
      }
      echo "<br/><br/><br/>";
      echo $lang['pages'] . ': ';
      while($order = mysql_fetch_array($query)) {
         if($f%7==0) {
            echo "<a href='index.php?start=$d'>$g</a>";
            $g++;
         }
         $d++;
         $f++;
      }
      ?>
   </div>
</div>

<?php
   include_once('inc/footer.php');
}
else {
   include_once('inc/offline_header.php');
   include_once('lib/stats_func.php');
?>

<div id="right_menu">
   <?php include_once('inc/sidebar_statistics.php'); ?>
   <br/>
   <?php include_once('inc/sidebar_follow.php'); ?>
</div>

<div id="content">
   <div class="text">
      <br/>
      <?php echo $lang['welcome']; ?>
      <br/><br/>
      <?php
      if (isset($_POST['submit'])) {
         $username = $_POST ['username'];
         $password = $_POST ['password'];
         $username = strip_tags ($username);   
         $password = md5 ($password);
         mysql_real_escape_string($username);
         $query = mysql_query ("SELECT id FROM users WHERE username='$username' AND password='$password'")
         or die ("Couldn't connect to the do database.") ;
         $result = mysql_fetch_array($query);
         if($result) {
            $_SESSION['username'] = $username;
            header('Location: index.php');
            if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
               $ip=$_SERVER['HTTP_CLIENT_IP'];
            }
            elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
               $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
            }
            else {
               $ip=$_SERVER['REMOTE_ADDR'];
            }
            $time = time();
            mysql_query ("INSERT into logins (userid, time, ip)
            VALUES ('$result[id]', '$time', '$ip')")
            or die ("Couldn't connect to the database.");
         }
         else {
            echo $lang['login_error'];
         }
         echo '<br/><br/>';         
      }
      ?>
      <h2 class="hr"><?php echo $lang['about']; ?></h2>
      <?php echo $lang['intro']; ?>
   </div>
</div>

<?php
}
   include_once('inc/footer.php');
?> 
Community
  • 1
  • 1
Florin Frătică
  • 587
  • 4
  • 7
  • 13
  • 2
    This is about the most commonly asked question here, and it's always the same cause. Please [search](http://stackoverflow.com/search?q=Cannot+modify+header+information+-+headers+already+sent+by) before asking. – deceze Apr 20 '11 at 08:36
  • 1
    @deceze Please read before closing. From what I understand he knows where the problem is. He's asking about the best way to handle it. So if you have a question similar to this please share, but don't link general "Header already sent" questions, as they are of no real use. – Alin Purcaru Apr 20 '11 at 08:39
  • 1
    @Alin And the answer is always the same: Don't start the session after content has been output. This question has been answered many times in just about every possible form already. – deceze Apr 20 '11 at 08:41
  • @deceze I'm sure it has been answered before, but please give a sample. And "Don't start the session after content has been output." may not be as easy as it sounds, at least in his case. – Alin Purcaru Apr 20 '11 at 08:45
  • @mario Why did you link a closed question? How does that help? Link an answered question, preferably with a few upvotes on the accepted answer. – Alin Purcaru Apr 20 '11 at 08:47
  • 1
    @Alin: Do not care. I if OP cannot be bothered to use the search, why would I search for a nice duplicate? Also, did *you* upvote this? – mario Apr 20 '11 at 08:49
  • @mario If you were bothered to add your input, at least do it properly. I up-voted only to offset an uncalled for down-vote. – Alin Purcaru Apr 20 '11 at 08:51
  • @Florin The answer to your question is included in the answer to this question: http://stackoverflow.com/questions/2832010/what-is-output-buffering – Alin Purcaru Apr 20 '11 at 08:55
  • @deceze: the problem isn't about starting the session. – Matt Ellen Apr 20 '11 at 08:56
  • @florian: your previous host probably used output buffering for all pages. have a look at [`ob_start()`](http://php.net/ob_start) to fix the problem on your new host – knittl Apr 20 '11 at 09:05

3 Answers3

1

You must have some echoed text before you've started the session. Try removing any of the output before session_start(), or move that function to offline_header.php

BenM
  • 52,573
  • 26
  • 113
  • 168
  • if the error was on line **2** then I would agree. The error occurs on line **84**, so is not related to starting the session. -1. – Matt Ellen Apr 20 '11 at 09:56
0

It would be great if You provided a code from the line 15 an before from the error generating file. Anyway this error is caused by output done before session_start() or other header() function is called.

It doesn't matter whether it was Your output (like echo, print, etc) it also could be an output from error message (Notice, Warning) that could be turned off at previous hosting but now they appear before You call some header function...

You should check Your code on the line 15 and also all the code before whether it is generating some error or there is direct output...

Hope it helps.

shadyyx
  • 15,825
  • 6
  • 60
  • 95
  • Please read the question. He knows where the error is and why. He's asking what to do next. Also if you take a look you'll see that PHP is used as an embedded language here so output is **all over the place**, not from an error or some missed whitespace. – Alin Purcaru Apr 20 '11 at 08:43
  • @Alin: he only thinks, he is not sure... I gave him a clue what to search for - output or notice/warning before the header() is called... And that is what he should do. Some output has to be done in other file, not in the one provided - error is caused by session_start(), but first output was probably here offline_header.php on line 15... – shadyyx Apr 20 '11 at 08:49
  • First output was in the main file `
    ` :) The error is given when a header cannot be set, because of previous output.
    – Alin Purcaru Apr 20 '11 at 08:53
0

As the error indicates, you cannot modify header information (which is what the header function does) once the header has already been sent. The headers get sent the first time you output text.

Before line 84 you have various places where text is output (for example lines 61, 63 and 65), and there could be more opportunities in the file which you include. This means that you cannot use the header function before theses lines, or possibly before any of the include calls in that else branch.

You should restructure your code to put

if (isset($_POST['submit'])) {...

as the first thing in the else branch, and don't output any html or other text until you know if the query succeeded, so that you know if you want to redirect to the index page.

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90