session_start()[function.session-start]:无法发送会话cookie - 已发送的标头

时间:2012-07-20 14:00:14

标签: php session

  

可能重复:
  Headers already sent by PHP

我是面向对象PHP的新手。此代码在函数php中运行良好,但它在OOP PHP中出现了一些错误。

当我打开index.php然后错误消息如下所示,但所有其他代码运行良好::

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

代码::

的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();
  ?>    

5 个答案:

答案 0 :(得分:2)

在调用session_start();之前,您不能包含直接发送给用户的任何。这包括空格,HTML,等等。

确保您的第一个<?php之前没有任何空格。确保在调用session_start后包含之后的标题。这应该可以解决问题。

答案 1 :(得分:0)

尝试更改require语句的顺序:

<?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");
 ?>

在将任何内容发送到浏览器之前,需要启动php会话。

答案 2 :(得分:0)

检查,您可能在创建会话之前提供输出的echoprint

答案 3 :(得分:0)

在index.php文件中,您正在向浏览器输出字符:

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

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

您必须将require_once文件移到此行上方或缓冲输出,直到您发送了所有标题。

答案 4 :(得分:0)

在调用session_start()之前不应生成任何输出。

index.php中,您加入了../Includes/initiate.php。在此文件中,您包含session.php,在其构造函数中调用session_start()。这会产生错误,因为您已经在index.php中生成了包含header.php的输出。