-2

Possible Duplicate:
Headers already sent by PHP

I am new in object-oriented PHP. This code works well in function php but it makes some error in OOP PHP.

When i open index.php then error message as like as show below but all other code works well ::

Warning: session_start() [function.session-start]: Cannot send session cookie - headers 
  already sent by (output started at 
  E:\XAMPP\xampp\htdocs\photo_gallery_new\public\layout\header.php:14) in 
  E:\XAMPP\xampp\htdocs\photo_gallery_new\Includes\session.php on line 15

  Warning: session_start() [function.session-start]: Cannot send session cache limiter -
   headers already sent (output started at 
  E:\XAMPP\xampp\htdocs\photo_gallery_new\public\layout\header.php:14) in
   E:\XAMPP\xampp\htdocs\photo_gallery_new\Includes\session.php on line 15

The code ::

index.php

 <?php  include ("../public/layout/header.php"); ?> // for design   header
<h2> Menu </h2>
 <?php

require_once ("../Includes/initiate.php");   // it makes error 
//require_once ("../Includes/user.php");    // it works 
//require_once ("../Includes/database_object.php");  // it makes error      

echo $user->full_name();
echo "<hr />";
  ?>
</div>   
  <?php  include ("../public/layout/footer.php");   ?>  // design for footer

user.php

<?php
require_once ("database.php");

 class USER extends  databaseObject {  // it makes error
//class USER {  // but it works         
     public $id; public $username; public $password;    public $firstName;
         public $lastName;
     // user authenticate   
     public static function authenticate( $username="", $password="" ) { ...  } 

     public function full_name()  {..... } // find full name
     // common database action
     public static function record_all_user() { ...  } // find all user

     public static function record_by_id($id=0) {.....} // find specific user

     public static function record_by_sql($sql="") { ...... }  // check sql
    ?>

header.php

 <html>
  <head>
 <title> Photo Gallery </title>
<link href="stylesheet/main.css" media="all" rel="stylesheet" type="text/css" />
  </head>
<body>
   <div id="header">
     <h1> PHOTO GALLERY </h1>
   </div>
<div id="main">

session.php

<?php
   class Session {          
    private $logged_in = FALSE;
    public $user_id;            
    function  __construct()  {
        session_start();
        $this->check_login();           
          .............................
            }

    public function is_logged_in() {....} // check loged in  

    public function login($user) { .....  }

    public function logout() {
        unset($_SESSION['user_id']);
        unset($this->user_id);
        $this->logged_in = FALSE;
    }

    private function check_login()  {... .}  // check login
}    

$session = new Session();
 ?> 

initiate.php

 <?php
  require_once ("Config.php");
require_once ("Functions.php");
require_once ("session.php");
require_once ("Database.php");
require_once ("database_object.php");  // this makes error
require_once ("user.php");
 ?>

database_object.php

 <?php
require_once ("database.php");
   class databaseObject {

}
$newClass = new databaseObject();
  ?>    
Community
  • 1
  • 1
sonnet
  • 13
  • 1
  • 5

5 Answers5

2

You can't include anything which gets sent directly to the user before calling session_start(); This includes whitespace, html, whatever.

Make sure that your first <?php does not have any whitespace before it. Make sure you are including the header after you call session_start. That should fix the problem.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
0

Try to change the order of your require-statements:

<?php
require_once ("session.php");
require_once ("Config.php");
require_once ("Functions.php");
require_once ("Database.php");
require_once ("database_object.php");  // this makes error
require_once ("user.php");
 ?>

The php session needs to be initiated before any content is sent to the browser.

ralphkurz
  • 1
  • 2
  • Output would still be created before the session creation because of the order of includes in index.php. `initiate.php` is called after `header.php`, which produces output. – ba0708 Jul 20 '12 at 14:09
  • Your're Right. Haven't seen it, yet. Sorry. – ralphkurz Jul 20 '12 at 14:13
0

Check , you may have echo , print that is giving output before your session created

Makesh
  • 1,236
  • 1
  • 11
  • 25
0

Right in your index.php file you are outputting characters to the browser:

 <?php  include ("../public/layout/header.php"); ?> // for design   header
<h2> Menu </h2>            <----- this line
 <?php

require_once ("../Includes/initiate.php");   // it makes error 

You have to either move the require_once files above this line or buffer the output until you've sent all the headers.

Patrick
  • 3,142
  • 4
  • 31
  • 46
0

No output should be produced prior to calling session_start().

In your index.php, you include ../Includes/initiate.php. In this file, you include session.php, which calls session_start() in its constructor. This produces an error because you have already produced output in index.php where you include header.php.

ba0708
  • 10,180
  • 13
  • 67
  • 99