3

Possible Duplicate:
Headers already sent by PHP

EDIT** I have now fixed the headers already sent up to the point where, line 53 ie. Last line in my common.php, which starts the session, is causing headers already sent. So now where am I supposed to put my session_start?

My index.php -

<?php 
require("common.php"); 
if(empty($_SESSION['user'])) 
{ 
    header("Location: login.php"); 
    die("Redirecting to login.php"); 
}   
?> 
<!DOCTYPE html>

<!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ -->
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
 <!--[if IE 7]>    <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]>    <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8" />

<!-- Set the viewport width to device width for mobile -->
<meta name="viewport" content="width=device-width" />

 <title>nCMS | Simplicity, Reimagined.</title>

 <!-- Included CSS Files (Uncompressed) -->
 <!--
 <link rel="stylesheet" href="stylesheets/foundation.css">
 -->

<!-- Included CSS Files (Compressed) -->
<link rel="stylesheet" href="stylesheets/foundation.min.css">
<link rel="stylesheet" href="stylesheets/app.css">

<script src="javascripts/modernizr.foundation.js"></script>

<!-- IE Fix for HTML5 Tags -->
<!--[if lt IE 9]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
 </head>
<body>
<!--INCLUDE HEADER -->

<?PHP include('header.php'); ?>


<!--/INCLUDE HEADER -->

<!--INCLUDE PAGE -->
<div class="row">
Hello <?php echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?>,     secret content!<br /> 


<h3>Hi, I'm Index!</h3>

</div>
 <!--/INCLUDE PAGE-->

  <!--INCLUDE FOOTER-->


<?PHP include('footer.php'); ?>

<!--/INCLUDE FOOTER-->
<!-- Included JS Files (Compressed) -->
<script src="javascripts/jquery.js"></script>
<script src="javascripts/foundation.min.js"></script>

<!-- Initialize JS Plugins -->
<script src="javascripts/app.js"></script>
</body>
</html>

This is the live page - http://www.cogameservers.com/ncms

Thank you for any help - Necro


common.php -

<?php 


$username = ""; 
$password = ""; 
$host = ""; 
$dbname = ""; 


$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 

try 
{ 

    $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 
} 
catch(PDOException $ex) 
{ 

    die("Failed to connect to the database: " . $ex->getMessage()); 
} 


$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 


$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 


if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) 
{ 
    function undo_magic_quotes_gpc(&$array) 
    { 
        foreach($array as &$value) 
        { 
            if(is_array($value)) 
            { 
                undo_magic_quotes_gpc($value); 
            } 
            else 
            { 
                $value = stripslashes($value); 
            } 
        } 
    } 

    undo_magic_quotes_gpc($_POST); 
    undo_magic_quotes_gpc($_GET); 
    undo_magic_quotes_gpc($_COOKIE); 
} 


header('Content-Type: text/html; charset=utf-8'); 


session_start();
Community
  • 1
  • 1
Necrohhh
  • 223
  • 2
  • 8
  • 13
  • What is on lines **9** and **42** in your `/home/megatron/public_html/ncms/index.php`? And also do you have any sort of output in `common.php`? – Havelock Sep 29 '12 at 17:31
  • Hello Necrohhh, you must know that every `header()` sent by PHP **SHOULD** be placed **BEFORE** the HTML code. To avoid this error. – Frederick Marcoux Sep 29 '12 at 17:32
  • can you paste your common.php code ? – GBD Sep 29 '12 at 17:32
  • The only output in common.php is a die message, and a header setting the charset to UTF8 - header('Content-Type: text/html; charset=utf-8'); Yes GBD Give me one sec. – Necrohhh Sep 29 '12 at 17:33
  • Check `common.php` for whitespace before the opening `` - even a trailing linebreak. That is a very common mistake. – Michael Berkowski Sep 29 '12 at 17:33
  • @MichaelBerkowski & GBD I just posted my common.php – Necrohhh Sep 29 '12 at 17:35
  • If you already have set *any* headers in `common.php` (so I understood `and a header setting the charset to UTF8 - header('Content-Type: text/html; charset=utf-8');`) no wonder about the error message *Cannot modify header information - headers already sent by .... in /home/megatron/public_html/ncms/common.php on line 77* – Havelock Sep 29 '12 at 17:36
  • Don't send the `Content-type text/html` header in common.php. It isn't needed, and indeed is causing your problem. If you must set the charset, do so _after_ your redirect. – Michael Berkowski Sep 29 '12 at 17:37
  • So what's the fix? How could I move those around so that the session is set, so that my redirection if not logged in code works? – Necrohhh Sep 29 '12 at 17:37
  • I just removed the Content type in common.php, now look at the live site at http://cogameservers.com/ncms , You can see now its throwing out issues with setting the session. – Necrohhh Sep 29 '12 at 17:39
  • Also check the *Related Section* of question on the right ;) --> – Havelock Sep 29 '12 at 17:40
  • It says now `output started at /home/megatron/public_html/ncms/index.php:9`... **Any** sort of output, i.e. `echo` before sending headers will break the magic – Havelock Sep 29 '12 at 17:41
  • Okay so now that I fixed that one, it is now saying that line 53 - ie. session_start(); is now considered the output that is breaking my headers? Where should I place session_start();??? – Necrohhh Sep 29 '12 at 17:43
  • Ok so I fixed the headers already sent, but now the session_start is supposedly the cause of the headers already sent in my common.php – Necrohhh Sep 29 '12 at 17:46
  • at starting of php call ob_start(); then call session_start() – GBD Sep 29 '12 at 17:47
  • and also open your common.php in notepad++ to check BOM side effects – GBD Sep 29 '12 at 17:48
  • Finally! Thanks so much to anyone who helped. This was making me pull the hair out of my head. Setting ob_start above of session_start in my common.php fixed that issue. – Necrohhh Sep 29 '12 at 18:03
  • you got this solution from my comment ? – GBD Sep 29 '12 at 18:06
  • Yes, the ob_start(); seemed to have solved this. – Necrohhh Sep 29 '12 at 18:13

2 Answers2

4

I know that you resolved your issue from my comments.but here it is as answer.

As you asked where you put session_start to remove header already sent error.

As general practice, put session_start() at the starting of your php file. also need to clear previous bufffer.

ob_start(); // this will clear output buffer
session_start();
GBD
  • 15,847
  • 2
  • 46
  • 50
3

It appears the error is caused by header("Location: login.php"); - the error says common.php is sending headers already on line 77. It will keep doing this as long as common.php is sending the headers.

Warning: Cannot modify header information - headers already sent by (output started at /home/megatron/public_html/ncms/index.php:9) in /home/megatron/public_html/ncms/common.php on line 77

Without knowing the contents of that file I can't help you. I'd suggest just looking through that file for something sending some headers and see if it necessary or approach this problem differently.

Hugo Scott-Slade
  • 896
  • 9
  • 16