-2

可能重复:
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

编码 ::

索引.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

用户.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
    ?>

头文件.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">

会话.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();
 ?> 

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

数据库对象.php

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

}
$newClass = new databaseObject();
  ?>    
4

5 回答 5

2

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

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

于 2012-07-20T14:04:16.513 回答
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 文件移到此行上方或缓冲输出,直到您发送了所有标题。

于 2012-07-20T14:06:35.187 回答
0

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

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

于 2012-07-20T14:07:05.923 回答
0

尝试更改您的要求语句的顺序:

<?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 会话。

于 2012-07-20T14:05:39.007 回答
0

检查,你可能有echoprint在你的会话创建之前给出输出

于 2012-07-20T14:05:42.093 回答