2012-07-20 73 views
-1

可能重複:
Headers already sent by PHP在session_start()[function.session啓動]:不能發送會話cookie - 標題已經發出已

我在面向對象的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(); 
    ?>  

回答

3

不能包括任何在調用之前直接發送給用戶session_start();這包括空格,html,等等。

確保您的第一個<?php之前沒有任何空格。確保你在之後包含標頭,你叫session_start。這應該解決問題。

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會話之前需要的任何內容發送到瀏覽器中啓動。

+0

由於index.php中包含的順序,輸出仍將在創建會話之前創建。在'header.php'之後調用'initiate.php',它會產生輸出。 – Andy0708 2012-07-20 14:09:54

+0

你是對的。還沒有看到它。抱歉。 – ralphkurz 2012-07-20 14:13:21

0

檢查,你可能有echoprint這是給你的輸出會在你的index.php文件中創建

0

右鍵,然後是輸出字符瀏覽器:

<?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文件移動到該行的上方或緩衝輸出,直到發送完所有標題。

+0

我編輯我的index.php :: <?php require_once(「../Includes/initiate.php」); ?> //然後其他代碼,它的作品 – sonnet 2012-07-20 17:08:27

0

在致電session_start()之前不應生產任何產品。

在您的index.php中,您包含../Includes/initiate.php。在此文件中,您包含session.php,它在其構造函數中調用session_start()。這會產生一個錯誤,因爲您已經在index.php中生成輸出,其中包含header.php

相關問題